在我的数据库中,我有一个字段,用逗号分隔的id列表(itemids)。
例如,我用测试数据填充一些行:
Row | itemids
1 => 2,5,8,11,22,45
2 => 4,6,9,13
3 => 1,3,5,16,23
4 => 6,12,18,21,24
现在我有一个搜索字符串,其中包含我想要搜索的所有id:
searchstring => 4,23,40
我现在要做的是从数据库中获取包含一个或多个值从我的搜索字符串的所有行。从我的例子中,我应该得到以下行:
Row | itemids
2 => 4,6,9,13
3 => 1,3,5,16,23
我该怎么做?你能给我一个SQL查询的例子吗?
谢谢你的帮助!
查询搜索字符串=> 4,23,40
select * from
item_table
where find_in_set(4,itemids)>0
UNION
select * from
item_table
where find_in_set(23,itemids)>0
UNION
select * from
item_table
where find_in_set(40,itemids)>0