如何通过自己的键值从json排序中获取所有键 例如:key是一个像uuid这样的正义数字,value是日期
我已经做到了,但是没有订购:
CREATE TABLE IF NOT EXISTS products
(
id text UNIQUE NOT NULL
CONSTRAINT crm_id_pkey PRIMARY KEY,
materials jsonb
);
select
jsonb_object_keys(materials) as key,
from
products
WHERE
id = '5'
所以我需要键的结果表,通过比较它们自己的日期(值)来排序:
select
jsonb_object_keys(materials) as key,
from
products
WHERE
id = '5'
ORDERED BY KEY.GETVALUE()
postgres中不存在按key.getValue()函数排序的命令,如果可以的话,我该如何进行排序?
更新: 我发现另一种解决方案不是更好,但也可以:
select jsonb_object_keys(materials) as keys,
materials -> jsonb_object_keys(materials) as date
from client_material
WHERE crm_id = '5'
order by date DESC;
答案 0 :(得分:0)
示例数据:
insert into products values
('5', '{"k1": "2019-05-01", "k2": "2019-04-01"}');
在横向联接中使用jsonb_object_keys(materials)
,以便可以在order by
子句中使用结果键:
select key
from products
cross join jsonb_object_keys(materials) as key
where id = '5'
order by (materials->>key)::date;
key
-----
k2
k1
(2 rows)