2011-05-19 123 views
3
I have two tables - `employee` and `department`. 

1. `employee` table contains column id,employee name and dept_id 
2. `department` table contains column id, department name. 

I need exact department name which contains 

1. maximum employee and 
2. no employee 

编辑帮助:我需要一个MySQL查询

Apologizing for bad grammar, here is the example for above two questions what i need. 
1. for eg: if two department contains same number of employees, i need to show both department not single by limit. 
2. for eg: if more than one department contains 0 employees, i must show those departments particularly. 
+0

@Harry喜悦:主题行说:“我需要MySQL查询帮助。” – mellamokb 2011-05-19 04:54:58

+0

他使用MySQL(注意标签)。那就是说,“最大雇员”和“没有雇员”是什么意思? – 2011-05-19 04:55:08

回答

2

回答第一个问题:

WITH epcount(dept_id, ep_count) AS 
(
    SELECT dept_id, COUNT(*) AS ep_count 
     FROM employee 
     GROUP BY dept_id 
) 
SELECT d.name FROM epcount AS ec1 JOIN department AS d ON ec1.dept_id=d.id 
    WHERE NOT EXISTS 
     (SELECT * FROM epcount AS ec2 WHERE ec1.ep_count < ec2.ep_count) 

回答了第二个问题:

SELECT name FROM department AS d 
    WHERE NOT EXISTS 
     (SELECT * FROM employee AS e WHERE d.id=e.dept_id) 
+0

我刚刚注意到提问者正在寻找MySQL解决方案。我对第一个查询的回答使用了一些SQL Server语法。除了WITH子句创建epcount临时表之外,任何方法都可以,关于如何编写这样的查询的WHERE子句的想法都是一样的。 – 2011-05-19 06:44:44

4
select department_name as `department name`, 
     count(*) as `number of employees` 
from employee 
     inner join department 
      on employee.dept_id = department.id 
group by department_name 
order by count(*) desc 
limit 1 

我认为应该这样做。我在一段时间内没有对mysql做任何事情。

编辑:错过了第二个问题

select department_name as `department name`, 
     count(*) as `number of employees` 
from employee 
     left join department 
      on employee.dept_id = department.id 
group by department_name 
    HAVING count(*) = 0 
+1

是不是有count(*)= 0而不是where? http://www.techonthenet.com/sql/having.php – 2011-05-19 05:03:37

+0

@Nikita Barsukov:你是对的 – 2011-05-19 05:07:15

+0

@Nikita,是的。谢谢。 – 2011-05-19 05:08:11

1

这将让你的部门的排序列表,员工数量排序。

SELECT `dept`.`id`, `dept`.`name`, COUNT(`employee`.`id`) as `employee_count` 
    FROM `dept` LEFT JOIN `employee` 
     ON `employee`.`dept_id` = `dept`.`id` 
    GROUP BY `dept`.`id` 
    ORDER BY `employee_count` 

要获得主管部门没有员工,添加:

AND `employee_count` = 0 

...在GROUP BY之前。

要获得员工人数最多的部门,请添加DESC LIMIT 1到最后。

+0

描述限制1到最后是不正确的。它目前返回包含最大雇员人数的第一个部门。不是所有包含该号码的部门。与employee_count子句一样,因为联接使其不返回任何行。 – 2011-05-19 05:10:08

+0

被编辑为使其成为左连接。问题表明,只有一个部门包含最大行数(尽管这可能只是OP的糟糕语法)。我承认这里的不准确,并根据他的需要来选择OP。顺便提一下,感谢您的更正! :) – 2011-05-19 05:12:59

0

查询,显示在它最大的员工和员工人数的部门名称:

SELECT department.name, COUNT(employee.name) from department 
INNER JOIN employee 
ON employee.dept_id = department.id 
GROUP BY department.name 
ORDER BY COUNT(employee.name) DESC limit 1 

查询,显示部门没有员工:

SELECT department.name from department 
LEFT JOIN employee 
ON employee.dept_id = department.id 
HAVING COUNT(employee.name) = 0 
GROUP BY department.name 

如果你需要显示它在一个查询,粘贴第一个查询,添加UNION ALL,然后粘贴第二个查询。

+0

第一个查询不正确。它目前返回包含最大雇员人数的第一个部门。不是所有包含该号码的部门。 – 2011-05-19 05:10:29

+0

@Denis - 在OP编辑他的问题后结果不正确。 http://stackoverflow.com/revisions/0b5ed70b-ac3c-4318-9d7c-de39e54b52a4/view-source – 2011-05-19 06:52:37

1

如果我读的问题的权利,你需要:

select department_name, 
     count(employee.dept_id) as num_employees 
from department 
left join employee on employee.dept_id = department.id 
group by department_name 
having count(employee.dept_id) = 0 or 
     count(employee.dept_id) = (select count(dept_id) 
        from employee 
        group by employee.id 
        order by count(dept_id) desc 
        limit 1) 
+0

我都喜欢,不喜欢这个答案。这正是欧普所要求的。 – 2011-05-19 05:09:45

+0

我觉得它也很丑。 :-) – 2011-05-19 05:12:20