21,893
社区成员




select productid, productname
from product
where find_in_set((select cids from product where productid = '$productid'), cids)
未经测试,如果不行,则先根据$productid查询出$cids,然后用$cids代替find_in_set函数的第一个参数即可。
如2楼所说,如果表记录很大,那么可能会很慢,建议还是改一下表结构。把productid和cid用单独一个表来记录对应关系,做好索引。select cids from product where productid = xxxx
得到的结果,比如是(1,3,4)在PHP中拆成数组,然后数据库中查询
select productid from product where ','+cids+',' like '%,1,%'
union
select productid from product where ','+cids+',' like '%,3,%'
union
select productid from product where ','+cids+',' like '%,4,%'
不直接like,而前后加逗号的原因是如果是12,13,14也会like1成功
另外这个写法如果表大了会很慢,因为会全表扫描,所以我还是建议你把表结构改一下。