假设以下 CTE:
with mytable as (
select column1 as foo, column2 as bar, column3 as baz
from (values
('a', 'b', 1),
('c', 'd', 2)
) v
)
使用array_agg()
输出值数组:
select
array_agg(v)
from mytable v;
-- {"(a,b,1)","(c,d,2)"}
但令人惊讶的是(至少对我而言),to_json()
在此数组上使用会将字段名称恢复为每一行的对象
select
to_json(array_agg(v))
from mytable v;
-- [{"foo":"a","bar":"b","baz":1},{"foo":"c","bar":"d","baz":2}]
我们如何让 PostgreSQL 输出一个数组数组,将每一行呈现为一个值数组?
select
something(v)
from mytable v;
-- [["a", "b", 1],["c", "d", 2]]