cho*_*sli 7 postgresql json aggregate aggregate-functions jsonb
使用Postgres-Db作为json文档的源代码,我需要将两列从表转换为JSON对象.
所以我在颜色表中有"color_id","language"和"name"列:
color_id | language | name
1 | "de" | "blau"
1 | "en" | "blue"
1 | "fr" | "bleu"
我想生成一个JSON对象,如:
{
"de": "blau",
"fr": "bleu",
"en": "blue"
}
我开始了
SELECT
array_to_json(array_agg((language::text, name::text))),
color_id
FROM colors
GROUP BY color_id;
不幸的是产生了
array to json | color_id
"[{"f1":"de","f2":"blau"} |
, {"f1":"en","f2":"blue"} | 1
, {"f1":"fr","f2":"bleu"}]" |
我认为这很简单 - 或多或少 - 但发现自己处于误导性结果和语法错误的死胡同.
亲切的问候,多米尼克
用途jsonb_object_agg():
with data(color_id, language, name) as (
values
(1, 'de', 'blau'),
(1, 'en', 'blue'),
(1, 'fr', 'bleu')
)
select color_id, jsonb_object_agg(language, name)
from data
group by 1;
color_id | jsonb_object_agg
----------+--------------------------------------------
1 | {"de": "blau", "en": "blue", "fr": "bleu"}
(1 row)
Run Code Online (Sandbox Code Playgroud)
该功能jsonb_object_agg()在Postgres 9.5中引入.
在Postgres 9.4中使用json_object_agg().
在Postgres 9.3中,您必须使用字符串函数构造结果:
select
color_id,
format('{%s}', string_agg(format('"%s": "%s"', language, name), ', '))::json
from data
group by 1;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2180 次 |
| 最近记录: |