将postgresql中的多行连接成单行

时间:2013-07-10 06:19:40

标签: postgresql

我有一个包含许多行的表

我希望将列的所有行连接到单行

例如

   columns
  -------
    a
    b
    c
    d
    e

我希望得到以下结果

    a,b,c,d,e

2 个答案:

答案 0 :(得分:2)

create table test (a text);

insert into test
values ('a'), ('b'), ('c'), ('d'), ('e');

select string_agg(a, ',') from test

SQL FIDDLE EXAMPLE

答案 1 :(得分:1)

SELECT 
array_to_string(array_agg("column"),',') AS yourColumn
FROM Table1

检查你的回答 demo

相关问题