[英]SELECT / Querying “into” JSON arrays
I have a PostgreSQL 9.4.5 database. 我有一个PostgreSQL 9.4.5数据库。 There's a table itemtable
with a JSONB column called data
, which contains sets of information like so (lots of stuff stripped for brevity): 有一个表itemtable
带有一个名为data
的JSONB列,其中包含类似这样的信息(为简洁起见,剥离了很多东西):
{ "items": [
{ "id": 158597 },
{ "id": 156300 },
(...)
] }
Now, I want to get a list of all the id
s in those sets of items - informally one might say SELECT id FROM items FROM data FROM itemtable
. 现在,我想获得这些项目集中所有id
的列表-非正式地,人们可能会说SELECT id FROM items FROM data FROM itemtable
。 I've only managed to do that using a subquery, which is obviously suboptimal: 我只能通过子查询来做到这一点,这显然不是最佳选择:
SELECT idlist->>'id' FROM (
SELECT json_array_elements(data::json->'items') AS idlist FROM itemtable
) AS necessary_subquery_name
How can I do that less convoluted? 我该如何减少麻烦呢?
Please note that I'd need to run this in SquirrelSQL, so the ::json
cast is needed. 请注意,我需要在SquirrelSQL中运行它,因此需要::json
。
Oh well, nevermind, I must have missed a coffee today. 哦,算了,我今天一定错过了一杯咖啡。 It's obviously: 显然是:
SELECT json_array_elements(data::json->'items')->>'id' FROM itemtable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.