一列应包含来自另一列的多行 table

A column should contain multiple rows from another table

id emp
1 乔恩
2 简


id 部门
1 小时
2 次销售


emp_id dept_id
1 1
2 1
2 2


结果
员工部
乔恩 ___ 小时
简 __ 小时,销售

我有员工 table、部门 table 和桥梁 table。

我如何编写查询,以便如果员工在多个部门,他们将显示在逗号分隔的 1 列中?

可以join这三个表,按员工聚合,每个员工的部门拼接成group_concat():

select e.emp, group_concat(d.dept) as depts
from emp e
inner join emp_dept ed on ed.emp_id = e.id
inner join dept d on d.id = ed.dept_id
group by e.id