Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql or/and precedence?

Tags:

I am wondering how or/and works?

For example if I want to get all rows where display = 1

I can just do WHERE tablename.display = 1

and if I want all rows where display = 1 or 2

I can just do WHERE tablename.display = 1 or tablename.display = 2

But what if I want to get all rows where display = 1 or 2 and where any of the content, tags, or title contains hello world

How would the logic play out for that?

Select * from tablename  where display = 1 or display = 2 and content like "%hello world%" or tags like "%hello world%" or title = "%hello world%" 

Would be my guess. but then I can read that in several ways.

Does it read out as:

 (display = 1 or display = 2) and (content like "%hello world%" or tags like "%hello world%" or title = "%hello world%") 

or as

((display = 1 or display = 2) and (content like "%hello world%")) or (tags like "%hello world%" or title = "%hello world%") 

etc.

like image 775
Hailwood Avatar asked Sep 10 '12 05:09

Hailwood


People also ask

Which takes precedence and or or in SQL?

The SQL Server AND operator takes precedence over the SQL Server OR operator (just like a multiplication operation takes precedence over an addition operation).

Which has higher precedence AND or or?

The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .

What is the difference in AND & or operator in MySQL?

The difference between AND, OR is that AND evaluates both conditions must be true for the overall condition to be true. The OR evaluates one condition must be true for the overall condition to be true. In the OR result, if name is John then condition will be true. If any row has the age 22, then it will be true.

Why does and have higher precedence than or?

Because in conventional mathematical notation, and (logical conjunction) has higher precedence than or (logical disjunction). All non-esoteric programming languages will reflect existing convention for this sort of thing, for obvious reasons.


1 Answers

The MySQL documentation has a good page with information on which operators take precedence.

From that page,

12.3.1. Operator Precedence

Operator precedences are shown in the following list, from highest precedence to the lowest. Operators that are shown together on a line have the same precedence.

INTERVAL BINARY, COLLATE ! - (unary minus), ~ (unary bit inversion) ^ *, /, DIV, %, MOD -, + <<, >> & | = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN BETWEEN, CASE, WHEN, THEN, ELSE NOT &&, AND XOR ||, OR = (assignment), := 

So your original query

Select     * from tablename  where     display = 1     or display = 2     and content like "%hello world%"     or tags like "%hello world%"     or title = "%hello world%" 

would be interpreted as

Select     * from tablename  where      (display = 1)     or (         (display = 2)         and (content like "%hello world%")     )     or (tags like "%hello world%")     or (title = "%hello world%") 

When in doubt, use parenthesis to make your intent clear. While the information on the MySQL page is helpful, it may not be immediately obvious if the query is ever revisited.

You might consider something like the following. Note that I've changed the title = "%hello world%" to title like "%hello world%", since that fits better with the goal you've described.

Select     * from tablename  where     (         (display = 1)         or (display = 2)     ) and (         (content like "%hello world%")         or (tags like "%hello world%")         or (title like "%hello world%")     ) 
like image 65
GargantuChet Avatar answered Nov 15 '22 19:11

GargantuChet