背景:现在要做三表联查,posts表和author表和ip表,其中user表的profile字段以jsonb类型存储在数据库,其类型是字典,nickname是其中的一个key,现要对nickname进行模糊查询,实现sql如下:
SELECT * FROM posts p LEFT JOIN users u ON p.poster_id=u.id AND p.poster_type='User' LEFT JOIN authors a ON p.poster_id = a.id AND p.poster_type='Author' LEFT JOIN ips i ON p.poster_id = i.id AND p.poster_type='IP' WHERE u.profile #>>'{nickname}' LIKE 'asd%' OR a.name LIKE 'asd%' OR i.name LIKE 'asd%';