![](/img/trans.png)
[英]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.