PostgreSQL 合并多行记录
此文转载至:https://www.cnblogs.com/jacker1979/p/4693707.html
需求如下:表table_xx 数据:
id | name |
1 | aaa |
1 | bbb |
2 | aa |
2 | bb |
1 | ccc |
3 | c |
需要转换成如下:
id | name |
1 | aaa,bbb,ccc |
2 | aa,bb |
3 | c |
查询语句如下:
SELECT a.id,array_to_string(array( SELECT b.name FROM table_xx b WHERE b.id=a.id ), ',') as name from table_xx a order by a.id
注:
其他行转列的方式 请参考:https://www.cnblogs.com/lurenjia1994/p/9535899.html