How to make an array from a SELECT returning more than one row

五迷三道 提交于 2019-12-23 22:30:25

问题


Is it possible to make one big array from a query like:

select
array_append(ARRAY[0], console_id)
from archive_sessions
where tournament_id = 14817

I tried with group by but I have to use console_id in it and it still is more than 1 row.

And how in this query initializing an empty ARRAY[]?


回答1:


You want array_agg

select array_agg(console_id) as consoles from archive_sessions where tournament_id = 14817



回答2:


If the query only returns column(s) that go into the array, use an ARRAY constructor:

SELECT ARRAY(SELECT console_id FROM archive_sessions
             WHERE  tournament_id = 14817) AS console_arr;

This is typically faster than array_agg() for the simple case.



来源:https://stackoverflow.com/questions/9896087/how-to-make-an-array-from-a-select-returning-more-than-one-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!