npc为许多别的id,这里用;间隔
create table users(
   npc varchar(30) not null
)insert into users values('11');insert into users values('11;22');
insert into users values('22;11122;3311;55');
我想要模糊查询出对应字段我想查出npc当中包含有id为11的数据,

11
11;22我可以给出基本答案,但是模糊查询部分有问题
select npc from users where if(LOCATE(";",npc )>0,npc like '%11;%',npc='11'); 
他会把第三条数据也查出来。我只想要id为11的数据,不想连包含数据也查出来,请问要怎么改

解决方案 »

  1.   

    SELECT * FROM users WHERE CONCAT(  ";", npc,  ";" ) LIKE  '%;11;%'
      

  2.   

    建议把;改成,如下insert into users values('22,11122,3311,55');然后你可以直接 select * from users where find_in_set(11,npc );如果不改,则可以
    select * from users where find_in_set(11,REPLACE(npc, ';', ',') );
      

  3.   

    select npc from users
    where npc like '%11%';
      

  4.   

    insert into users values(';11;22');
    insert into users values(';22;11122;3311;55');
    select npc from users
    where npc like concat(';',11,';');
      

  5.   

    FIND_IN_SET(str,strlist) 
    假如字符串str 在由N 子链组成的字符串列表strlist 中, 则返回值的范围在 1 到 N 之间 。一个字符串列表就是一个由一些被‘,’符号分开的自链组成的字符串。如果第一个参数是一个常数字符串,而第二个是type SET列,则   FIND_IN_SET() 函数被优化,使用比特计算。如果str不在strlist 或strlist 为空字符串,则返回值为 0 。如任意一个参数为NULL,则返回值为 NULL。 这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运行。  mysql> SELECT FIND_IN_SET('b','a,b,c,d');        -> 2
      

  6.   

    or
    insert into users values('11;22');
    insert into users values('22;11122;3311;55');
    select npc from users
    where concat(';',npc,';') like concat(';',11,';');