简体   繁体   English

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

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

So I have a Postgres SQL table that has a column of the type jsonb array.所以我有一个 Postgres SQL 表,其中有一列类型为 jsonb 数组。 Looks like this more or less:或多或少看起来像这样:

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

So I want to select rows that only have animals with status ACTIVE in column 2.所以我想 select 行只有第 2 列中状态为 ACTIVE 的动物。

Any ideas of how to go about it?关于如何 go 的任何想法?

Thank you!谢谢!

Assuming the only two values are ACTIVE and INACTIVE you can use a SQL JSON/Path expression:假设只有两个值是ACTIVEINACTIVE ,您可以使用 SQL JSON/Path 表达式:

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

Another option is the contains operator @>另一个选项是包含运算符@>

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

This returns all rows that do not contain a status with INACTIVE这将返回所有不包含状态为 INACTIVE 的行

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

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