sql - 在postgres中查询以表格形式查看jsonb数据列的动态值
问题描述
在“快照”表中,我按以下顺序排列
id(uuid) || snapShotId(uuid) || resultData(jsonb) || createdAt(timestamp)
“resultData”列中的数据是 jsonb 类型,它存储的动态数据如下:
[{id: 1, name: 'Ram', age: 23, }]
[{id:2, title: 'Some title', release_year: '1995'}, {id:3, title: 'Some title 1', release_year: '1996'}]
[{id:4, email: 'hello@gmail.com'}]
我想要的是编写一个查询来查看具有表格格式动态值的“resultData”列的列:
结果将如下所示:
**id || name || age || title || email || release_year**
1 || Ram || 23 || null || null || null
2 || null || null || some title || null || 1995
3 || null || null || some title 1 || null || 1996
4 || null || null || null || hello@gmail.com || null
解决方案
您可以jsonb_array_elements
为此使用:
select r.item ->> 'id' as id,
r.item ->> 'name' as name,
r.item ->> 'age' as age,
r.item ->> 'title' as title,
r.item ->> 'email' as email,
r.item ->> 'release_year' as release_year
from snapshot
cross join jsonb_array_elements(result_data) as r(item)
推荐阅读
- c++ - ASan:在递归函数中 vector.emplace(push)_back 之后的堆使用后释放
- ios - 如何在没有 Cocoapods 的情况下在我的颤振应用程序中获取 webview_flutter 插件
- python - 如何将值转换为列并将其他列中的值放入熊猫中的这些列中
- angularjs - 在 Rxjs 的 map 运算符中,大括号内的 body 和圆括号内的 body 代表什么?
- python - 如何停止遍历枚举列表的循环?
- python - 如何在下面的代码中打印字典中的值?
- php - 为什么我收到以下错误“第 1 行的列‘模型’的数据被截断”
- google-apps-script - 为什么在我的 for 循环中尝试减少 i 时会出现语法错误“Unexpected Identifier”?
- haskell - 使用类型同义词定义实例
- json - 在 vb.net 中获取满足特定条件的 JObject 的所有键