MySql排除查询

时间:2016-11-22 20:11:34

标签: mysql

我有一张看起来像这样的表

JOB_ID  | Email        | Type       | Date      
_____________________________________
28319   | mike@aol.com | Service    | 11-24-2016
_____________________________________ 
28412   | mike@aol.com | Rotation   | 11-24-2016
_____________________________________
38123   | andy@aol.com | Service    | 11-24-2016
_____________________________________
28199   | roger@aol.com| Service    | 11-24-2016

我将运行哪些查询来返回那些已安排服务但在特定日期没有轮换的人。

这是我的尝试:

SELECT j.Job_ID,j.Email,j.Type,j.Date
from Jobs j 
join
     Jobs  j2
     on j.Email = j2.Customer_Email
where j2.JOB_ID NOT IN (select j.JOB_ID
                  from Jobs j
                  where j.Type = 'Rotation'
                 )     AND j.Date = '11-24-2016';

所以我的查询将返回andy和roger。

1 个答案:

答案 0 :(得分:1)

这应该可以帮到你,不需要加入:

SELECT * FROM Jobs WHERE Date = '11-24-2016' AND Type = 'Service' AND 
Email NOT IN (SELECT Email FROM Jobs WHERE Date = '11-24-2016' AND 
Type = 'Rotation');