面试题:要求用一句sql 查询出 每个部门所含的员工人数,没有的也要显示出来!
department 部门表,employee员工表,
查询结果如下图所示:
首先将需要的字段先列出来,将employee的数量结果做出一个新的表格以 t为别名 后续以left join 进行分类 然后以 部门编号排序 得出结果
select c.id AS '部门编号',c.`name` AS '部门名称', t.Num AS '人员数量' from department as c
left join
(select id_department,count(*) AS Num from employee group by id_department)
as t on c.id= t.id_department
以上就是查询语句 最近在复习一些知识,准备跳槽