如何在MySQL中使用count函数的结果?

时间:2013-03-09 17:37:16

标签: mysql sql

我需要做一个查询,计算每个部门有多少员工,员工人数超过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;

除了关于拥有超过1名员工的部分之外,这还满足所有要求,是否有办法仅对第一行返回2或更高的结果执行其余代码?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

试试这个

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