jsonb_path_query 按 child 订购
jsonb_path_query with order by child
我有一个嵌套的 json 结构
{
"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"
}
}
}
}
我想 select /feed/3
中的所有内容并按 created
订购。
我一直想不出正确的语法。
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;
这个版本给了我一个ERROR: column "created" does not exist
没有 order by 子句,输出看起来像
# 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_query 中的 side-effect 返回了一组,但我不知道如何处理它。感谢您的帮助!
您的结果不包含名为 created
的列,仅包含包含 JSON 值的名为 jsonb_path_query
的列。
您需要从 JSON 中提取数字才能对其进行排序。还建议在 FROM 子句中使用集合返回函数:
SELECT *
FROM jsonb_path_query('{...}', '$."feed"."3".*') as t(item)
order by (item ->> 'created')::bigint desc;
我有一个嵌套的 json 结构
{
"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"
}
}
}
}
我想 select /feed/3
中的所有内容并按 created
订购。
我一直想不出正确的语法。
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;
这个版本给了我一个ERROR: column "created" does not exist
没有 order by 子句,输出看起来像
# 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_query 中的 side-effect 返回了一组,但我不知道如何处理它。感谢您的帮助!
您的结果不包含名为 created
的列,仅包含包含 JSON 值的名为 jsonb_path_query
的列。
您需要从 JSON 中提取数字才能对其进行排序。还建议在 FROM 子句中使用集合返回函数:
SELECT *
FROM jsonb_path_query('{...}', '$."feed"."3".*') as t(item)
order by (item ->> 'created')::bigint desc;