我有2个表员工和工资 Employe => ID,名称字段
薪资=> Sid,EId(Foriegn key),月,薪水
Employee
ID Name
1 a
2 b
3 c
Salary
Sid Eid Month Salary
1 1 Jan 10
2 2 Jan 10
3 3 Jan 10
4 1 Feb 10
5 3 Feb 10
6 1 Mar 10
7 2 Mar 10
需要找到在3月份没有工资的员工和仅使用加入,因为我需要优化
我使用子查询搜索stmt
select E.Name from Employee where E.ID not in (Select EID from salary where month ='mar' );
为了优化目的,我被要求将其转换为加入
我尝试使用
Select E.Name from Employee E left join Salary S on E.ID = S.EID where S.EID = null;
但这不是我想要的,我只需要那些在三月月份没有获得工资的员工。
答案 0 :(得分:4)
试试这个:
SELECT E.Name
FROM Employee E
LEFT JOIN salary S
ON E.Id = S.EID
AND S.month ='mar'
GROUP BY E.Name
HAVING COUNT(E.Id) != COUNT(S.EID)
<强> SQL FIDDLE DEMO 强>
答案 1 :(得分:1)
SELECT E.Name
FROM Employee E
LEFT JOIN salary S
ON E.Id = S.EID
AND S.month ='mar'
GROUP BY E.Id
HAVING COUNT(E.Id) != COUNT(S.EID)
试试这个。