Home  >  Article  >  Backend Development  >  sql查询的相关有关问题

sql查询的相关有关问题

WBOY
WBOYOriginal
2016-06-13 13:33:14749browse

sql查询的相关问题
表结构
 id name nasid
  1 nas1 1,2,3
  2 nas2 2,3,4
  3 nas3 2,3,5


我现在有个两个nasid:1,2
我想查询上面三条数据中nasid包含1,2(至少包含一个数字)的所有数据

这个sql该怎么写呢,请各位高手给点思路,尝试过like,但是效率太低

------解决方案--------------------
select * from 表 where find_in_set('1',nasid) or find_in_set('2',nasid)
------解决方案--------------------
全表扫描,效率高不了的
------解决方案--------------------
用正则找吧。。


SELECT * FROM `2012` WHERE `2012` REGEXP '(1|2)+'

刚测了一下,你可到我系统test数据库中执行一下看看
------解决方案--------------------
正则简单一点的还行,稍复杂的一点的就相当不给力了。有现在的函数当然得用现成的函数.
to amani11:既然说到全表扫描的问题,你是怎么做的呢?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn