【问题标题】:How to make an array from a SELECT returning more than one row如何从返回多行的 SELECT 中创建一个数组
【发布时间】:2012-03-27 19:13:46
【问题描述】:

是否可以从如下查询中创建一个大数组:

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

我尝试使用 group by,但我必须在其中使用 console_id,它仍然超过 1 行。

在这个查询中如何初始化一个空的ARRAY[]

【问题讨论】:

    标签: sql arrays postgresql aggregate


    【解决方案1】:

    如果查询只返回进入数组的列,请使用ARRAY constructor

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

    对于简单的情况,这通常比array_agg()

    【讨论】:

      【解决方案2】:

      你想要array_agg

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

      【讨论】:

      • 我见过这个功能,但为什么我没有尝试它-1为我+1为你
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2023-03-19
      • 2015-06-05
      • 2023-02-26
      相关资源
      最近更新 更多