gpt4 book ai didi

sql - 如何在postgres中将记录集转换为json数组

转载 作者:行者123 更新时间:2023-11-29 11:58:53 26 4
gpt4 key购买 nike

我有一张 table

  name | age 
abc | 20
pqr | 30

我想要像 json 数组一样的结果

{
[{
"name":"abc",
"age":20
},
{
"name":"pqr",
"age":30
}]
}

我知道他们的方法

row_to_json();

那只会给我单行 json 但我想要数组,请帮我解决这个问题

最佳答案

select json_agg(row_to_json(t))
from t
;
json_agg
----------------------------------------------------
[{"name":"abc","age":20}, {"name":"pqr","age":30}]

关于sql - 如何在postgres中将记录集转换为json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com