2011-05-19 208 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