MySQL 某字段分组后,其他字段以逗号隔开

建表

CREATE TABLE student (
age INT,
name VARCHAR(20)
)

造数据

INSERT INTO 
student(age, name)
VALUES
(15, '陈x'),
(16, '冯xx'),
(15, '郑xx'),
(16, '王xx'),
(15, '刘xx')
age name
15 陈x
16 冯xx
15 郑xx
16 王xx
15 刘xx

实现

SELECT 
age, GROUP_CONCAT(name SEPARATOR ',') AS names
FROM
student
GROUP BY
age
age names
15 陈x,郑xx,刘xx
16 冯xx,王xx