我需要做一个查询,计算每个部门中有多少员工拥有超过1名员工,并且员工编号为6或更高,我有:

 Select DepartmentName, count(EmployeeNumber) as "Number of Employees"
 From department d, employee e
 Where d.DepartmentName = e.Department and e.EmployeeNumber <=6
 Group by d.DepartmentName;

这就满足了所有的需求,除了关于拥有一个以上员工的部分,是否有方法只对从第一行返回2个或更多的结果执行其余的代码?

最佳答案

使用HAVING子句。但请注意,它是在计算完整件事之后应用的。

Select DepartmentName, count(EmployeeNumber) as "Number of Employees"
From department d, employee e
Where d.DepartmentName = e.Department and e.EmployeeNumber <=6
Group by d.DepartmentName HAVING count(EmployeeNumber) > 1;

关于mysql - 如何在MySQL中使用count函数的结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15313812/

10-13 02:08