Postgresql 按子级排序的jsonb_路径_查询
我有一个嵌套的json结构Postgresql 按子级排序的jsonb_路径_查询,postgresql,jsonb,Postgresql,Jsonb,我有一个嵌套的json结构 { "feed": { "3": { "id1": { "id": "activity-1", "created": 1469905513973, "verb": "added recipe 1" },
{
"feed": {
"3": {
"id1": {
"id": "activity-1",
"created": 1469905513973,
"verb": "added recipe 1"
},
"id2": {
"id": "activity-2",
"created": 1470013085119,
"verb": "added recipe 2"
},
"id3": {
"id": "activity-3",
"created": 1472341861543,
"verb": "added recipe 3"
}
}
}
}
我想选择/feed/3
中的所有内容,并按创建的订购
我一直无法理解正确的语法
SELECT jsonb_path_query(
'{ "feed": { "3": { "id1": { "id": "activity-1", "created": 1469905513973, "verb": "added recipe 1" }, "id2": { "id": "activity-2", "created": 1470013085119, "verb": "added recipe 2" }, "id3": { "id": "activity-3", "created": 1472341861543, "verb": "added recipe 3" } } } }',
'$."feed"."3".*')
order by "created" desc;
此版本给了我一个错误:列“created”不存在
如果没有ORDERBY子句,则输出如下
# SELECT jsonb_path_query('{ "feed": { "3": { "id1": { "id": "activity-1", "created": 1469905513973, "verb": "added recipe 1" }, "id2": { "id": "activity-2", "created": 1470013085119, "verb": "added recipe 2" }, "id3": { "id": "activity-3", "created": 1472341861543, "verb": "added recipe 3" } } } }', '$."feed"."3".*');
jsonb_path_query
--------------------------------------------------------------------------
{"id": "activity-1", "verb": "added recipe 1", "created": 1469905513973}
{"id": "activity-2", "verb": "added recipe 2", "created": 1470013085119}
{"id": "activity-3", "verb": "added recipe 3", "created": 1472341861543}
(3 rows)
我很确定这是jsonb_path_查询返回集合的副作用,但我不知道如何处理它。谢谢你的帮助 您的结果不包含名为created
的列,只包含名为jsonb\u path\u query
的列,该列包含JSON值
您需要从JSON中提取数字,以便能够对其进行排序。还建议在FROM子句中使用set返回函数:
SELECT *
FROM jsonb_path_query('{...}', '$."feed"."3".*') as t(item)
order by (item ->> 'created')::bigint desc;