MySQL where子句不返回预期值

时间:2017-06-06 05:43:12

标签: mysql

我有一个查询

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; 

但它返回的行id为3,而filename不在where子句中。这有什么问题?

1 个答案:

答案 0 :(得分:1)

我相信db.getCollection('CollectionName').find( { $and: [{ "users.type" : "user" }, { "users.status" : "2"}, {"users.userId" : ObjectId("Some Object Id")} ]} 在MySQL(以及大多数数据库)中的优先级高于AND,因此您当前的查询会将文件名视为可选的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 *子句中出现的列或集合函数内的列才有资格在查询中进行选择。