2017-10-04 133 views
0

我們正在嘗試構建一個查詢,以篩選出標識爲SuspendedInactive(這些值字符串顯示在我們的數據表中'status列'中)的僱員。如果員工具有以上任一狀態,他們將顯示在On Hold數據表中,而不是Fail數據表中。MySQL查詢複合語句不返回預期結果

在下面的情況下,「李四」做失敗了Approval,而是因爲她是Inactive她應該被處理爲On Hold並沒有出現在Fail數據表。

我們預計這些結果在Fail表當我們運行下面的查詢:

Employee ID FirstName LastName Status Eligibility Approved 
    123456  Jim  James  OK   OK  Not Approved 
    777889  Ray  Raymond OK   Ineligible  OK 

但是相反,我們看到:

Employee ID FirstName LastName Status Eligibility Approved 
    123456  Jim  James  OK   OK  Not Approved 
    123412  Jane  Doe  Inactive  OK  Not Approved 
    777889  Ray  Raymond OK  Ineligible  OK 

下面是我們運行查詢,但Inactive員工繼續儘管出現了第一個複合語句(我們在此數據運行中沒有任何Suspended僱員,如果這有所作爲),也會出現。

SELECT 
    emp_id, 
    first_name, 
    last_name, 
    status, 
    eligibility, 
    approved 
FROM 
    `pass_fail` 
WHERE(
     status <> 'Inactive' 
OR 
     status <> 'Suspended' 
) 
AND (
     eligibility ='Ineligible' 
OR 
    approved = 'Not Approved') 

回答

0

更改或和兩個狀態條件

之間
SELECT emp_id, first_name, last_name, status, eligibility, approved 
FROM `pass_fail` 
WHERE status <> 'Inactive' 
    AND status <> 'Suspended' 
    AND (eligibility ='Ineligible' OR approved = 'Not Approved') 
1

我會用innot in。按照你的邏輯,查詢將是:

SELECT emp_id, first_name, last_name, status, eligibility, approved 
FROM pass_fail 
WHERE status NOT IN ('Inactive', 'Suspended'); 

您的文字不介紹如何eligibilityapproved都應該使用。但我推測:

SELECT emp_id, first_name, last_name, status, eligibility, approved 
FROM pass_fail 
WHERE status NOT IN ('Inactive', 'Suspended') AND 
     (eligibility = 'Ineligible' OR approved = 'Not Approved'); 
+0

謝謝戈登!我們測試了您的解決方案並且效果很好。湯姆 – tomish