【问题标题】:Select Rows postgres SQL where Key = String for all elements of Jsonb Array为 Jsonb 数组的所有元素选择 Rows postgres SQL where Key = String
【发布时间】:2022-01-18 23:11:05
【问题描述】:

所以我有一个 Postgres SQL 表,其中有一列类型为 jsonb 数组。或多或少看起来像这样:

[{"adi": "cat", "status": "ACTIVE"}, {"adi": "dog", "status": "ACTIVE"}, {"adi": "bird", "status": "INACTIVE"}]
[{"adi": "fish", "status": "ACTIVE"}, {"adi": "dog", "status": "ACTIVE"}, {"adi": "reptile", "status": "ACTIVE"}]

所以我想在第 2 列中选择只有状态为 ACTIVE 的动物的行。

你有什么想法吗?

谢谢!

【问题讨论】:

  • 那么您使用的是 Postgress 还是 MySQL 数据库?
  • postgres 感谢您删除标签
  • 请不要发送垃圾邮件标签,它们可以为您的问题吸引合适的受众。如果你只是发送一两个标签,你只会收集一个愤怒的皇冠,上面有燃烧的手杖和干草叉

标签: sql postgresql jsonb


【解决方案1】:

假设只有两个值是 ACTIVEINACTIVE,您可以使用 SQL JSON/Path 表达式:

select *
from the_table
where not the_column @@ '$[*].status == "INACTIVE"'

另一个选项是包含运算符@>

select *
from the_table
where not the_column @> '[{"status": "INACTIVE"}]'

这将返回所有不包含状态为 INACTIVE 的行

【讨论】:

    猜你喜欢
    • 2021-01-08
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多