【问题标题】:trouble filtering jsonb data in postgresql在 postgresql 中过滤 jsonb 数据时遇到问题
【发布时间】:2018-10-18 16:50:34
【问题描述】:

我正在尝试学习如何过滤 jsonb 数据。当我跑步时:

SELECT DISTINCT jsonb_array_elements(data) 
FROM reports 
WHERE data @> '[{"status": "Active"}]'

而不是只返回 status: Active 的行,它似乎忽略了 WHERE 子句,我还返回包含 status: Inactive 的行

寻求帮助以了解这里发生了什么。

数据看起来像

[{"report": "Report1", "status": "Active"},
 {"report": "Report2", "status": "Inactive"},
 {"report": "Report3", "status": "Inactive"},
 {"report": "Report4", "status": "Active"}]

【问题讨论】:

    标签: sql postgresql jsonb


    【解决方案1】:
    select * from reports
    join lateral jsonb_array_elements(data) j(v)
    on true
    where
    v->>'status' = 'Active'
    

    这里 v 是来自 jsonb 数组的元素,例如{"report": "Report1", "status": "Active"}

    然后使用->> 运算符过滤“状态”键,其中值为“活动”

    【讨论】:

      猜你喜欢
      • 2021-04-13
      • 2018-12-13
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 2021-10-21
      相关资源
      最近更新 更多