如何在Postgres的不同行中连接字符串 [英] How to concat strings in different row on Postgres
本文介绍了如何在Postgres的不同行中连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个表 Foo
,并有一列 name
。我想将 Foo
中的所有名称连接起来。例如,
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
Table Foo
Name
---------
name1
name2
name3
我想编写一个查询,该查询返回 name1name2name3
或返回 name1,name2,name3
的查询。
I want to write a query that returns name1name2name3
or if possible name1,name2,name3
.
我已经进行了一些谷歌搜索,看到了 concat
函数,但它只合并同一行的列。我找不到实现此功能的函数或方法。
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.
推荐答案
使用string_agg
use string_agg
SELECT string_agg(Foo, ', ') AS col
FROM tbl
这篇关于如何在Postgres的不同行中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文