如何修改查询以获得预期结果,如下所示?
表格
id name posts
1 A
2 B 3
3 C 1,2
4 D 3,1
5 E 4,1,2
QUERY
SELECT * FROM table
WHERE id IN (SELECT posts FROM table WHERE id = 5)
RESULT
id name posts
4 D 3,1
预期结果
id name posts
1 A
2 B 3
4 D 3,1
答案 0 :(得分:2)
使用find_in_set()
:
SELECT *
FROM table t
WHERE EXISTS (SELECT 1 FROM table t2 WHERE t2.id = 5 and find_in_set(t.id, t2.posts) > 0);
然后着手修复数据库结构。