SQL:
SELECT group_concat(name,',',remark order by id desc separator ';') FROM test group by age;
运行结果:
2.oracle:
wmsys.wm_concat函数,功能比较简单,只能实现单字段间的合并,demo如下:
表数据:
SQL:
SELECT DISTINCT
wmsys.wm_concat (NAME) OVER (
PARTITION BY AGE
ORDER BY
(SELECT 1 FROM dual)
)
FROM
(
SELECT
*
FROM
T_TEST
ORDER BY
ID DESC
) T
结果:
3.Postgresql:array_to_string以及
string_agg两个函数都能实现,推荐string_agg,demo如下:
表数据:
SQL:
SELECT id,array_to_string(ARRAY(SELECT unnest(array_agg(name)) order by 1),';') FROM t_kenyon GROUP BY id ORDER BY id;
SELECT id,string_agg(name,';') FROM t_kenyon GROUP BY id ORDER BY id;
结果: