现在有一个ids字段是'11,22,33',是另外一张表的id。
但是在检索的时候 id IN (ids),这里的ids是作为一个字符串,
而不是‘11’,‘22’,‘33’.
mysql中有什么方法吗

解决方案 »

  1.   

    用FIND_IN_SETselect * from table1
    where FIND_IN_SET(id,ids)
      

  2.   


    delimiter //
    drop procedure if exists spFind;
    CREATE PROCEDURE  `spFind`(ids varchar(30))
    begin
    set @s=concat('select * from t2 where id in(',ids,')');
    select @s;
    prepare stmt1 from @s;
    execute stmt1;
    DEALLOCATE PREPARE stmt1;
    end;
    //
    delimiter ;存储过程;
    127.0.0.1~root@localhost~test>select * from t2;
    +----+-----+-------+
    | id | id1 | sname |
    +----+-----+-------+
    |  3 |   2 | a     |
    |  4 |   3 | a     |
    |  8 |   4 | a     |
    |  1 |  10 | a     |
    +----+-----+-------+
    4 rows in set (0.00 sec)127.0.0.1~root@localhost~test>call spFind('4,3');
    +-----------------------------------+
    | @s                                |
    +-----------------------------------+
    | select * from t2 where id in(4,3) |
    +-----------------------------------+
    1 row in set (0.00 sec)+----+-----+-------+
    | id | id1 | sname |
    +----+-----+-------+
    |  3 |   2 | a     |
    |  4 |   3 | a     |
    +----+-----+-------+
    2 rows in set (0.00 sec)Query OK, 0 rows affected (0.00 sec)