繁体   English   中英

Select 行 postgres SQL 其中 Key = Jsonb 数组的所有元素的字符串

[英]Select Rows postgres SQL where Key = String for all elements of Jsonb Array

所以我有一个 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"}]

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

关于如何 go 的任何想法?

谢谢!

假设只有两个值是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 的行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM