2017-04-22 59 views
0

加入我想找到數最多的員工數據庫在職職工的部門經理的職務是其結構data structure複雜的子查詢,並在MySQL

SELECT title ,dept_name 
FROM titles 
JOIN dept_manager as j ON titles.emp_no = j.emp_no 
JOIN (SELECT y.dept_no,EmployeeCount FROM departments as y JOIN (SELECT 
count(*) as EmployeeCount, dept_no FROM dept_emp WHERE to_date = '9999-01- 
01' GROUP BY dept_no ORDER BY EmployeeCount Desc Limit 1) AS n ON y.dept_no 
= n.dept_no) 
AS s ON j.dept_no = s.dept_no limit 1; 

這必須是查詢它根據它,但它不工作我得到迷茫普萊舍找幫我

+0

沒有人會爲你解決任務。我建議你嘗試通過子任務來分離整個任務,並向前邁進,以使一切正確。 – hastrb

回答

0

首先,你希望員工在各部門人數:

select de.dept_name, count(*) 
from dept_emp de 
where de.to_date = '9999-01-01' 
group by de.dept_name; 

然後,order by count(*) desc limit 1獲得員工人數最多的部門。注意:您不指定如何處理關係,因此如果有關係,這隻會得到僱員最多的任意部門。

您可以通過加入dept_managertitles來輕鬆擴展此功能。這就是我會建議這樣做的。

+0

這就是我想要做的,但它變得複雜 –

0
SELECT title,dept_name 
from departments as k 
join (
SELECT title ,j.dept_no 
FROM titles as a 
JOIN dept_manager as j ON a.emp_no = j.emp_no 
JOIN (SELECT count(*) as EmployeeCount, dept_no 
    FROM dept_emp WHERE to_date = '9999-01-01' 
    GROUP BY dept_no ORDER BY EmployeeCount Desc Limit 1) 
AS s ON j.dept_no = s.dept_no limit 1) as j on k.dept_no = j.dept_no; 

這是按預期打印輸出。