Sql 查询以在postgres中以表格形式查看具有动态值的jsonb数据列
在“快照”表中,我有以下顺序的行Sql 查询以在postgres中以表格形式查看具有动态值的jsonb数据列,sql,json,postgresql,Sql,Json,Postgresql,在“快照”表中,我有以下顺序的行 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', re
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'}]
**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
我想要的是编写一个查询来查看“resultData”列的列,该列具有表格式的动态值:
结果如下所示:
[{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'}]
**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_数组_元素
:
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)
一个选项是使用
JSONB\u POPULATE\u RECORDSET()函数
SELECT t.*
FROM snapshot
CROSS JOIN JSONB_POPULATE_RECORDSET( NULL::record, jsdata )
AS t( id INT , name VARCHAR(80), age INT,
title VARCHAR(80), email VARCHAR(80), release_year INT )
通过正确(双重)引用当前JSON值来修复语法后
列中的数据是动态的,可以有任何键和值。。所以我们不能像上面提到的那样进行查询。查询必须知道值,我们不需要指定它。列中的数据是动态的,可以有任何键和值。。所以我们不能像上面提到的那样进行查询。查询必须知道值,我们不需要指定它。@SabinBogati:不可能有“动态列”。SQL语言的一个限制是,在开始查询之前(即解析SQL时),必须知道查询的所有列(包括它们的数据类型)。