簡體   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