Select 数据作为 JSONB,其中值用作 json 键
Select data as JSONB where value used as json key
100500 次我在这里找到答案,但现在没有。
我有 PostgreSQL 11.1 和 table
CREATE TABLE public.bots_script_elements (
id integer,
icon text,
CONSTRAINT bots_script_elements_pri PRIMARY KEY (id)
);
值
ID ICON
1 "begin"
2 "form"
3 "calendar"
如何select 数据如下json?
{
"1": {"id":1, "icon":"begin"},
"2": {"id":2, "icon":"form"},
"3": {"id":3, "icon":"calendar"}
}
Json 对象键 1、2 和 3 是 ID 列中的值。
使用聚合函数jsonb_object_agg():
select jsonb_object_agg(id, to_jsonb(b))
from bots_script_elements b
100500 次我在这里找到答案,但现在没有。
我有 PostgreSQL 11.1 和 table
CREATE TABLE public.bots_script_elements (
id integer,
icon text,
CONSTRAINT bots_script_elements_pri PRIMARY KEY (id)
);
值
ID ICON
1 "begin"
2 "form"
3 "calendar"
如何select 数据如下json?
{
"1": {"id":1, "icon":"begin"},
"2": {"id":2, "icon":"form"},
"3": {"id":3, "icon":"calendar"}
}
Json 对象键 1、2 和 3 是 ID 列中的值。
使用聚合函数jsonb_object_agg():
select jsonb_object_agg(id, to_jsonb(b))
from bots_script_elements b