2012-04-04 59 views
0

嗨我有以下的sql語句給我first_name和每個員工已經完成的所有工作總數,但現在我想要有first_namejob_id員工正在與另一個工作工人我有三個表共sql有功能

下面是我用來獲得名字和職位總數一共代碼

SELECT first_name, COUNT(link.job_id) 
FROM employee, job, link 
WHERE job.job_id = link.job_id 
AND employee.employee_id = link.employee_id 
GROUP BY first_name 
HAVING (COUNT(link.job_id) > 1) ; 

有人可以幫助請

回答

2

如何:

SELECT first_name, COUNT(link.job_id) 
FROM employee, job, link 
WHERE job.job_id = link.job_id 
AND employee.employee_id = link.employee_id 
AND job.job_id IN (SELECT job_id FROM link GROUP BY job_id HAVING COUNT(*) > 1) 
GROUP BY first_name 
HAVING (COUNT(link.job_id) > 1) ; 

子查詢將獲得涉及多個員工的所有工作。

+0

感謝您的幫助 – suryll 2012-04-04 09:03:37