簡體   English   中英

SQL-查詢JSON中的哈希內的數組元素

[英]SQL - Query for array elements inside hash in json

我有一個具有以下給定結構的jsonb列的表。

my_json:{“ records”:[{“ id”:1,“ name”:“ first”},{“ id”:2,“ name”:“ second”}},{“ id”:3,“ name” :“第三”}]}

我需要的是選擇記錄密鑰中存在的所有ID。 而且我在這個json結構的任何地方都找不到任何幫助。

我已經嘗試過以下查詢,但實際上無法達到我想要達到的目標。

"select my_json->>'records' from my_table where my_json->>'records' is not null AND my_json #> '{records}' != '[]'"

這實際上給出了完整的數組,但是我需要的是其中所有'id'鍵的值。

請有人幫忙解決一下。

注意:我使用Postgres 9.5

您尋找json_array_elements

t=# with c(my_json) as (values (' {"records": [{"id":1, "name":"first"}, {"id":2, "name":"second"}, {"id":3, "name":"third"}]}'::json))
select json_array_elements(my_json->'records')->>'id' from c;
 ?column?
----------
 1
 2
 3
(3 rows)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM