如何`MySQL`在`WHERE`子句中定义顺序?

时间:2014-06-24 09:56:42

标签: mysql where where-clause logical-operators

请有人解释为什么查询:

WHERE property_id = 1 OR property_id = 2
    AND property_id = 3

WHERE (property_id = 1 OR property_id = 2)
    AND property_id = 3

给出不同的结果?

MySQL如何在WHERE子句中定义顺序?

2 个答案:

答案 0 :(得分:3)

由于AND / OR逻辑运算符的优先级

第一个查询等同于

WHERE property_id = 1 OR (property_id = 2
    AND property_id = 3)

因为AND的优先级高于OR运算符

答案 1 :(得分:2)

检查Manual Here是否有运营商优先权

在您的第一个查询where子句中检查此条件 由于property_id = 2 AND property_id = 3运算符优先于AND运算符,因此OR

在第二个查询where子句中首先检查此条件(property_id = 1 OR property_id = 2),因为括号比其他比较运算符具有更多优先级。

您的第一个查询等同于

WHERE property_id = 1 OR (property_id = 2
    AND property_id = 3)