[英]Postgres select rows from table where variable in JSONB array
[英]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 的任何想法?
谢谢!
假设只有两个值是ACTIVE
和INACTIVE
,您可以使用 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.