查询条件:   
  $sort_id   =   '11,21,23';   
    
  查询表:   
  id  sort   
  1   11,22   
  2   21,23   
  3   50,60   
  4   23,60   
    
  查询结果:   
  id  sort   
  1   11,22   
  2   21,23   
  4   23,60   
    
  ID   3   ,没有任何匹配,所以未被列出。   
    
  请问这样的MYSQL语句应该怎么写? 

解决方案 »

  1.   

    这个可以利用mySQL中的regexp正则匹配来实现。select * from yourTable
    where sort REGEXP concat('[' & replace('11,21,23',','|') & ']');http://dev.mysql.com/doc/refman/5.1/zh/functions.html#string-comparison-functions
      

  2.   

    用MYSQL的REGEXP就可以
    select * from yourTable where sort REGEXP 
    concat('[', replace('11,21,23',','|'), ']');
      

  3.   

    为什么mysql 报告ERROR: 引号不配对 @ 86 呢?
      

  4.   

    select * from aa where sort REGEXP
    concat('[', replace('11,21,23',',','|'), ']'); 
      

  5.   

    不对啊,怎么查出来的数据里还有别的数据呢?比如我这样写
    select * from aa where sort REGEXP 
    concat('[', replace('11,21,23',',','|'), ']'); 目的是要查出sort字段(字符串类型)中所有包含'11,21,23',可查出来的sort数据中还有其它的数据而不是我要的'11,21,23'另外我的数据库都是utf8格式的
      

  6.   

    我的数据库中 sort字段有以下数据:共17条
           1,11,19 
          1,8,26 
          1,8,9 
          1,11,20 
          1,2,3,0 
          1,8,26 
          1,8,26 
          1,8,0 
          1,2,3,29,30 
          1,8,26 
          1,2,7 
          1,12,25 
          1,2,0 
          1,8,0 
          1,2,3,29,30 
          1,2,3,29,0 
          1,2,3,29,30 SQL这样写:select * from tab where sort REGEXP concat('[', replace('8,11,',',','|'), ']');  目的是想查出字符串中包含8,11的所有数据,可结果如下:
    1,11,19 
          1,8,26 
          1,8,9 
          1,11,20 
          1,2,3,0 
          1,8,26 
          1,8,26 
          1,8,0 
          1,2,3,29,30 
          1,8,26 
          1,2,7 
          1,12,25 
          1,2,0 
          1,8,0 
          1,2,3,29,30 
          1,2,3,29,0 
          1,2,3,29,30 
    后面的3条和上面几条不是我要的数据啊
      

  7.   

    select * from 
    concat(',',aa,',') where sort REGEXP
    concat('[', replace('11,21,23',',','|'), ']'); 
      

  8.   

    select * from tab 
    where sort REGEXP concat('[', replace('8,11',',','|'), ']');  你多了个逗号.
    replace('8,11,',',','|')
    -->>
    replace('8,11',',','|')
      

  9.   

    select * from tab 
    where concat(',',sort,',') REGEXP concat(replace('8,11',',','|')); 
      

  10.   

    select * from tab 
    where sort REGEXP concat(replace('8,11',',','|')); 
      

  11.   

    还是有问题啊,where sort REGEXP concat(replace('2,29',',','|'))
    结果连 1,8,26 这个也查出来了
      

  12.   

    select '1,9,28' REGEXP concat(replace('2,29',',','|'));上面的好像不对啊,目的是要查出含 2和29的,但好像上面的返回了1,请高手指点。
    把'2,29'改成'3,29'就可以返回0