MySQL OR/AND Precedence
Understanding operator precedence is crucial when crafting MySQL queries. In the example provided, you wanted to select rows where display = 1 or 2 and where any of the content, tags, or title contains "hello world."
According to MySQL documentation, the precedence of the logical operators is as follows:
Therefore, the query you provided would be interpreted as:
(display = 1) OR ( (display = 2) AND (content like "%hello world%") ) OR (tags like "%hello world%") OR (title like "%hello world%")
To ensure your intentions are clear, consider using parentheses explicitly, especially when dealing with complex expressions. A more explicit version of the query might look like this:
( (display = 1) OR (display = 2) ) AND ( (content like "%hello world%") OR (tags like "%hello world%") OR (title like "%hello world%") )
This makes it unambiguous that the OR and AND operators apply as intended.
The above is the detailed content of How Does MySQL Handle OR and AND Operators in Queries?. For more information, please follow other related articles on the PHP Chinese website!