例子:一个用户用字符串记录多个 poi 信息(可以有多个相同用户),查询该用户未关联的 poi 信息。如果用户不存在则查出所有的 poi。
表 poi(id, name)
1,故宫
2,长城
3,天坛
4,水立方
表 user(id, poi_ids)
1, 1,2
1, 3
2, 1,2,7,9
select poi.id, poi.name from poi where !find_in_set(poi.id, IFNULL( (select GROUP_CONCAT(DISTINCT user.poi_ids) from user where user.id = 1 ),'-1') );
GROUP_CONCAT 查到了用户ID为1的所有 poi_ids, 结果为1,2,3
IFNULL 如果结果集为空时赋默认值
find_in_set 查某个列(如 poi 的id)在某个拼接的字符串中(如user 的 poi_ids的1,2,3)