Database:PostgreSQL 9.4.24
报错信息显示:函数 group_concat(character varying) 不存在,通过查资料发现。

PostgreSQL里面没有group_concat函数(MySQL里面有—),为了使用group_concat这一功能,我们可以用array_agg 和
array_to_string 取而代之。array_agg 和 array_to_string同样可以用于其他数据库,用于达到group_concat这一功能。

string_agg函数可以替代array_agg 和 array_to_string的组合。

STRING_AGG(DISTINCT CASE WHEN party_role_type = 0 THEN role_name END, ',') AS "zero_party"
**结论Conclusion**

There is no function like group_concat available in PostgreSQL which was available in MySQL, instead of using this function we are using array_agg and array_to_string function. Array_agg and array_to_string function will work same as group_concat function work in other databases. We can also use string_agg function in PostgreSQL.

Example

分隔符separator可以自己选
select address, array_to_string(array_agg(name order by name), '%') from group_concat group by address;
select address, array_to_string(array_agg(name order by name), ',') from group_concat group by address;
# 若要组内去重
select address, array_to_string(array_agg(distinct name order by name), ',') from group_concat group by address;

https://www.educba.com/postgresql-group_concat/

PostgreSQL查看版本信息

https://blog.csdn.net/kmblack1/article/details/78721653

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐