如果我有一个这样的表:
office_id int
employees jsonb
数据看起来像这样:
1
[{ "name" : "John" }, { "name" : "Jane" }]
有没有一种简单的方法来查询,使结果看起来像这样:
name,office_id
John,1
Jane,1
我一直在阅读json functions似乎这是可能的,但我似乎无法弄清楚。我宁愿不必在每个嵌套对象上存储 office_id。
最佳答案
您可以使用json_array_elements
来扩展json数组
select id , json_array_elements(employees)->>'name' from mytable
关于sql - Postgres 9.4 : Include sibling column in each jsonb array result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33717084/