Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where 'foo' OR 'bar' AND 'lol' OR 'rofl' MySQL

In what order would this be evaluated. My intension is that if it finds either foo or bar, it would also search for lol and rofl.
Is this totally in the woods? And if so, how would one evaluate an expression like that.

like image 541
Mathias Vielwerth Avatar asked Dec 21 '22 20:12

Mathias Vielwerth


1 Answers

The AND operator has higher precedence than OR in MySql, so your current expression evaluates as:

WHERE 'foo' OR ('bar' AND 'lol') OR 'rofl'

Add parentheses to the expression if you want to force the evaluation order:

WHERE ('foo' OR 'bar') AND ('lol' OR 'rofl')
like image 174
LukeH Avatar answered Jan 02 '23 23:01

LukeH