如表table 包含以下字段
id      csite     ....
1       1,2,13    .... 
2       2,3,4     ....
3       1,4,5     ....
4       2,5,23    ....
5       1,3,8     ....假设取得一个数字变量3,怎样列出包含数字3的所有数据行使用select id,csite from table where  csite like '%3%' or csite like '%3,%' or csite like '%,3,%' or csite like '%,3%'这样行不通,会把13,23的也查出来想要的结果是:
id      csite     ....
2       2,3,4     ....
5       1,3,8     ....查过一些资料,用这个charindex(3,csite)>0试了不管用,该怎么办??不要太复杂sql语句和存储过程谢谢!!!!!

解决方案 »

  1.   

    select id,csite from table where  csite = '3' or csite like '%3,%' or csite like '%,3,%' or csite like '%,3%' 可行,有其他方法吗?
      

  2.   

    楼上的应该也可以把这样的数据提出来:
    13,1,2
    2,1,31
    ...
    不合要求的数据...select id,csite from table where  csite = '3' or csite like '3,%' or csite like '%,3,%' or csite like '%,3' 
      

  3.   

    $sql = "select id,csite from table where $num in(csite)"; $num为你要查的值。。
      

  4.   


    是啊,有没有谁帮帮忙呀,还有就是数据库表里的字段中含有html标签怎么去掉标签后使用like去模糊查询??
      

  5.   

    可以使用MySQL自己的函数FIND_IN_SETFIND_IN_SET(str,strlist) 
    Returns a value 如果字符串 str 在由 N 个子串组成的列表 strlist 中,返回一个 1 到 N 的值。一个字符串列表是由通过字符 “,” 分隔的多个子串组成。如果第一个参数是一个常数字符串,并且第二个参数是一个 SET 列类型,FIND_IN_SET() 函数将被优化为使用位运算!如果 str 在不 strlist 中或者如果 strlist 是一个空串,返回值为 0。如果任何一个参数为 NULL,返回值也是 NULL。如果第一个参数包含一个 “,”,这个函数将完全不能工作: 
    mysql> SELECT FIND_IN_SET('b','a,b,c,d');
            -> 2
      

  6.   

    官方文档http://dev.mysql.com/doc/refman/5.0/en/string-functions.html