问题
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