[英]SQL - Query for array elements inside hash in json
I have a table with a jsonb column of the below given structure. 我有一个具有以下给定结构的jsonb列的表。
my_json: {"records": [{"id":1, "name":"first"}, {"id":2, "name":"second"}, {"id":3, "name":"third"}]}
my_json:{“ records”:[{“ id”:1,“ name”:“ first”},{“ id”:2,“ name”:“ second”}},{“ id”:3,“ name” :“第三”}]}
What i need is select all the ids present inside the records key. 我需要的是选择记录密钥中存在的所有ID。 And I couldn't find any help anywhere for this json structure.
而且我在这个json结构的任何地方都找不到任何帮助。
I have tried for the following queries, but couldn't actually get to a point that i want to reach. 我已经尝试过以下查询,但实际上无法达到我想要达到的目标。
"select my_json->>'records' from my_table where my_json->>'records' is not null AND my_json #> '{records}' != '[]'"
this actually gives the complete array, but what i need is the value of all the 'id' key in that. 这实际上给出了完整的数组,但是我需要的是其中所有'id'键的值。
could somebody help in sorting it out please. 请有人帮忙解决一下。
Note: I use Postgres 9.5 注意:我使用Postgres 9.5
you look for json_array_elements
: 您寻找
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.