数据库
首页 > 数据库> > postgresql模糊查询json类型字段内某一属性值

postgresql模糊查询json类型字段内某一属性值

作者:互联网

背景:现在要做三表联查,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%';

标签:nickname,postgresql,poster,字段,asd%,json,JOIN,type,id
来源: https://www.cnblogs.com/miaoweiye/p/12179082.html