How to concat strings in different row on Postgres

蓝咒 提交于 2020-01-22 02:35:07

问题


Let's say I have a table Foo and has a column name. I want to concatenate all names in Foo. For example

Table Foo

  Name
---------
  name1
  name2
  name3

I want to write a query that returns name1name2name3 or if possible name1,name2,name3.

I have done some googling and see concat function but it only concats columns of same row. I couldn't find a function or a way to accomplish this.


回答1:


use string_agg

SELECT string_agg(Foo, ', ') AS col
FROM   tbl


来源:https://stackoverflow.com/questions/58743015/how-to-concat-strings-in-different-row-on-postgres

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