select * from class;
select * from student;
class表
id name
1 三年二班
2 三年一班
student表
id name class_id
1 小明 1
2 小红 2
3 小兰 2
注:group_concat(id separator '-'); separator可以指定连接符,默认为,
select c.name,(select group_concat(name) from student s where s.class_id =c.id ) from class
c
输出结果:
三年二班 小明
三年一班 小红,小兰
文章来源于:https://blog.csdn.net/textalign/article/details/71746973