1

I know its recomended to use parenthesis to separate and ,or statements.but I'm wondering how mysql engine does render statements without parenthesis.lets say we have this statement:

select * from users where A and B or C and D;

how would it be with parenthesis?

Mat
  • 202,337
  • 40
  • 393
  • 406
alex
  • 7,551
  • 13
  • 48
  • 80
  • 1
    **[Operator Precedence](https://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html)** First `AND` then `OR`. Same as in algebra `A*B + C*D` – Lukasz Szozda Mar 12 '16 at 13:16
  • 1
    Possible duplicate of [Mysql or/and precedence?](http://stackoverflow.com/questions/12345569/mysql-or-and-precedence) – SOFe Mar 12 '16 at 13:28

3 Answers3

4

AND has higher priority than OR.

select * from users where (A and B) or (C and D);

Refer to: http://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html

Dylan Su
  • 5,975
  • 1
  • 16
  • 25
4

It is like in math expressions:

AND is like a PRODUCT
OR is like a SUM

so AND are executed before OR, unless differently indicated through parenthesis, like in math again.

White Feather
  • 2,733
  • 1
  • 15
  • 21
1

AND has higher precedence than OR.

Priority of AND and OR operator in Mysql select query

Operator Precedence

Community
  • 1
  • 1
Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42