如上图所示数据表
怎么读出A中的数据呢?(同时也要读出B中对应的数据)
------解决方案--------------------
select * from `a` ,`b` where `a`.`bid` like concat('%',`b`.`bid`,'%')
如果要查a表id等于1对应的关联数据,可以写成
select * from `a` ,`b` where `a`.`id`='1' and `a`.`bid` like concat('%',`b`.`bid`,'%')
------解决方案--------------------
CREATE TEMPORARY TABLE a (id int, bid varchar(10), content varchar(10));
INSERT INTO a VALUES('1','1,5,2','AAAAAAA');
INSERT INTO a VALUES('2','2,4','AAAAAAA');
INSERT INTO a VALUES('3','1,3,2','AAAAAAA');
CREATE TEMPORARY TABLE b (bid int, content varchar(10));
INSERT INTO b VALUES('1','BBBBB');
INSERT INTO b VALUES('2','BBBBB');
INSERT INTO b VALUES('3','BBBBB');
INSERT INTO b VALUES('4','BBBBB');
INSERT INTO b VALUES('5','BBBBB');
select a.id, b.bid as bid, a.content, b.content as bcontent
from a, b
where find_in_set(b.bid, a.bid)
order by a.id