2016-11-22 87 views
1

我有一個表,看起來像這樣MySql的排除查詢

JOB_ID | Email  | Type  | Date  
_____________________________________ 
28319 | [email protected] | Service | 11-24-2016 
_____________________________________ 
28412 | [email protected] | Rotation | 11-24-2016 
_____________________________________ 
38123 | [email protected] | Service | 11-24-2016 
_____________________________________ 
28199 | [email protected]| 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'; 

所以我的查詢將返回安迪和羅傑。

+0

作爲,使用適當的數據類型 – Strawberry

回答

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'); 
+0

有什麼不對的JOIN一定要存儲日期? – Strawberry

+0

@Strawberry JOIN絕對沒有錯 – Reto