网站是个多城市的,发布新闻的时候,可以选择哪些城市显示,字段 city 经常是这样显示2,5,66,77
5,12,34,12MSSQL里的charindex在mysql里用不了,我想问问,如果我想查询 city  字段里 带 5 的,如何查询呢?select * from table instr(','+city+',',',5,')>0这样查询查不到

解决方案 »

  1.   

    select *
    from `table`
    where locate(',5,',concat(',',city,','))>0
      

  2.   


    mysql> select instr('2,5,66,77','5');
    +------------------------+
    | instr('2,5,66,77','5') |
    +------------------------+
    |                      3 |
    +------------------------+
    1 row in set (0.00 sec)mysql> select instr('5,12,34,12','5');
    +-------------------------+
    | instr('5,12,34,12','5') |
    +-------------------------+
    |                       1 |
    +-------------------------+
    1 row in set (0.00 sec)mysql> select instr('5,12,34,12','6');
    +-------------------------+
    | instr('5,12,34,12','6') |
    +-------------------------+
    |                       0 |
    +-------------------------+
    1 row in set (0.00 sec)mysql>得到的这个值大于0,代表有5这个CITY。
    select * from table instr(city,'5')>0
      

  3.   

    你可以使用FIND_IN_SETmysql> SELECT FIND_IN_SET('b','a,b,c,d');
            -> 2
      

  4.   

    1、
    select * from table instr(CONCAT(',',city,','),',5,')>0
    2、
    select * from table FIND_IN_SET('5',city)>0