有一表,其中有2列(列a,列b)。列a为序号,列b中存储数据为如下格式,其中","为人为设定的间隔符
a          b
1         1,2,3,44,555,77,8
2         22,4,6,8
3         22,4,99,55
4         44,3,22
如何通过一条sql语句查出b中字段同时含有22,4的所有记录

解决方案 »

  1.   

    select * from 表
    where charindex(',22,', ',' + b + ',') > 0 and charindex(',4,' ',' + b + ',') > 0
      

  2.   

    select * from tb where charindex(',22,' , ',' + b + ',') > 0 and charindex(',4,' , ',' + b + ',') > 0
      

  3.   

    select * from 表 where charindex(',22,',','+b+',')>0 and charindex(',4,',','+b+',')>0
      

  4.   

    --这两种都行.
    select * from tb where charindex(',22,' , ',' + b + ',') > 0 and charindex(',4,' , ',' + b + ',') > 0select * from tb where ',' + b + ',' like '%,22,%' and ',' + b + ',' like '%,4,%'
      

  5.   

    谢谢大家,问题解决了,发现LS给出的答案当字段中只为"22,4"时,该记录无法被查询出,故做了如下修改
    select * from table1 where charindex('22' , ',' + aaa + ',') > 0 and charindex('4' , ',' + aaa + ',') > 0
    声明,此帖并非想考大家,而是真的忘记用charindex这个函数了,并且调试中发现了如上问题,进行了改正