postgresql行转列并拼接字符串

有这样一张表:

select * from liutest order by 1;

id |   kw   
----+--------
  1 | big
  1 | hello
  2 | oracle
  2 | small
  2 | apple
  3 | shit
(6 rows)

 

我想按id把kw拼接成一个字符串,在网上查到一种方法:

select id,
array_to_string
        (
        ARRAY   (
                SELECT  kw
                FROM    liutest gi
                WHERE   gi.id = gd.id
                ),
        ', '
        ) AS group_concat
FROM    liutest gd
ORDER BY
        id;

 

posted on 2016-06-13 15:56  手撕高达的村长  阅读(1211)  评论(0编辑  收藏  举报

导航