Victor Bocharsky
Victor Bocharsky

Reputation: 12306

How `MySQL` define order in `WHERE` clause?

Please, somebody explain me why queries:

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

and

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

given different results?

And how MySQL define order in WHERE clause?

Upvotes: 0

Views: 71

Answers (2)

Sadikhasan
Sadikhasan

Reputation: 18600

Check Manual Here for Operator Precedence

In your first query where clause check this condition property_id = 2 AND property_id = 3 because of AND operator is more precedence then OR Operator.

In second query where clause check first this condtion (property_id = 1 OR property_id = 2) because bracket have more precedence then other compare operator.

Your first query equivalent to

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

Upvotes: 2

mavroprovato
mavroprovato

Reputation: 8362

Because of the precedence of the AND/OR logical operators

The first query is equivalent to

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

Because AND has higher precedence than OR operator

Upvotes: 3

Related Questions