请考虑两个表-员工和部门
Employee:
EmployeeID
Name
DeptID
Department:
DepartmentID
DeptName
LocID
Employee.DeptID is a foreign key to Department.DepartmentID
如何显示所有部门的列表(部门名称)以及每个部门的雇员人数?输出应如下所示:
DepartmentName Number of employees
Accounts 30
HR 24
Production 400
Sales/Marketing 250
etc...
最佳答案
使用GROUP BY
SELECT d.deptID, count(e.deptID)
FROM Department d
LEFT JOIN Employee e ON d.DeptID = e.DeptID
GROUP BY d.deptId
LEFT JOIN
用于包括没有员工的部门。关于mysql - 如何使用COUNT()和GROUP BY显示每个部门的员 worker 数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46350881/