mysql中有group_concat聚集函数,与group by一起使用。按某个字段的分组将分组中特定列的值拼成字符串。
select group_concat(name) from student group by class;/*按班级号分组,将同一个班的学生姓名拼起来,用逗号间隔*/
postgre没有现成的group_concat函数,只能自定义:
CREATE AGGREGATE group_concat(anyelement)
(
sfunc = array_append, -- 每行的操作函数,将本行append到数组里
stype = anyarray, -- 聚集后返回数组类型
initcond = '{}' -- 初始化空数组
);
如果想删除聚集函数,可以用:
drop aggregate group_concat(anyelement);