纵向合并
Mysql
GROUP_CONCAT函数用法:
GROUP_CONCAT( 合并的字段名 order by 排序字段 SEPARATOR 分割符)
方式一:只合并
GROUP_CONCAT( stu.NAME )
方式二:带排序
GROUP_CONCAT( stu.NAME ORDER BY s.score DESC )
方式三:带分割符
GROUP_CONCAT( stu.NAME ORDER BY s.score DESC SEPARATOR ',' )
-- 根据成绩排名 并使用,分割
SELECT
s.cid,
c.NAME,
GROUP_CONCAT( stu.NAME ORDER BY s.score DESC SEPARATOR ',' )
FROM
student stu
LEFT JOIN score s ON s.sid = stu.id
LEFT JOIN course c ON s.cid = c.id
GROUP BY
s.cid,
c.NAME
Oracle
listagg within GROUP 用法:
listagg ( 合并的字段名, '分割符' ) within GROUP ( ORDER BY 排序字段 DESC )
-- 根据成绩排名 并使用,分割
SELECT
s.cid,
c.NAME,
listagg ( stu.NAME, ',' ) within GROUP ( ORDER BY s.SCORE DESC )
FROM
student stu
LEFT JOIN score s ON s.sid = stu.id
LEFT JOIN course c ON s.cid = c.id
GROUP BY
s.cid,
c.NAME
标签:GROUP,NAME,cid,stu,&&,Mysql,Oracle,ORDER,CONCAT
From: https://blog.51cto.com/u_16085354/6397787