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

相关文章:

  • 2022-12-23
  • 2021-08-29
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-01-31
猜你喜欢
  • 2021-11-18
  • 2022-12-23
  • 2021-12-10
  • 2021-09-17
  • 2021-10-16
  • 2022-12-23
相关资源
相似解决方案