I have a PostgreSQL 9.4.5 database. There's a table itemtable
with a JSONB column called data
, which contains sets of information like so (lots of stuff stripped for brevity):
{ "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
. 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.