2017-06-06 92 views
0

我有一個查詢MySQL的where子句沒有返回預期值

SELECT * 
FROM hayabusa.customer_staging 
WHERE tin = "888-596-592" 
     OR gsis_sss_no = "bp0279638" 
     OR last_name LIKE "%dela cruz%" 
     OR first_name LIKE "%jose%" 
     OR date_of_birth = "8/8/1978" 
     OR gender = "male" 
      AND parent_id = 0 
      AND is_proccessed = 0 
      AND id > 27 
      AND (filename = "sample cif csv file - copy.csv" 
        OR filename = "sample cif csv file2.csv") 
GROUP BY tin, 
      gsis_sss_no, 
      last_name, 
      first_name, 
      date_of_birth, 
      gender 
ORDER BY CASE 
      WHEN tin = "888-596-592" THEN 1 
      ELSE 2 
      end, 
      id 
LIMIT 1; 

但它返回一排3的idfilename這不是where子句。它有什麼問題?

+0

當您使用OR和AND時,請始終使用禁忌符號。嘗試添加這些,一切都應該很好 –

+0

你能分享你認爲不正確的完整記錄嗎? –

回答

1

我認爲AND在MySQL(和大多數數據庫)中的優先級高於OR,所以你當前的查詢將把你的AND條件視爲可選的文件名。嘗試重寫WHERE條款,因爲這:

WHERE 
    (tin = "888-596-592" 
    OR gsis_sss_no = "bp0279638" 
    OR last_name LIKE "%dela cruz%" 
    OR first_name LIKE "%jose%" 
    OR date_of_birth = "8/8/1978" 
    OR gender = "male") 
     AND parent_id = 0 
     AND is_proccessed = 0 
     AND id > 27 
     AND (filename = "sample cif csv file - copy.csv" 
       OR filename = "sample cif csv file2.csv") 

另外,我不知道你打算選擇哪些列,但使用SELECT *這裏可能是不合適的。相反,你應該列出你想要選擇的列。嚴格地說,只有GROUP BY子句中出現的列或聚合函數內部的列纔有資格在查詢中進行選擇。

+0

謝謝!有效! –