Select 行 postgres SQL 其中 Key = Jsonb 数组所有元素的字符串
Select Rows postgres SQL where Key = String for all elements of Jsonb Array
所以我有一个 Postgres SQL table,它有一列类型为 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 的动物的行。
有什么想法吗?
谢谢!
假设仅有的两个值是 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"}]'
这 returns 所有不包含 INACTIVE
状态的行
所以我有一个 Postgres SQL table,它有一列类型为 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 的动物的行。
有什么想法吗?
谢谢!
假设仅有的两个值是 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"}]'
这 returns 所有不包含 INACTIVE
状态的行