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
  • 195,986
  • 40
  • 382
  • 396
alex
  • 6,771
  • 8
  • 44
  • 71
  • 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,875
  • 1
  • 15
  • 23
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,615
  • 1
  • 14
  • 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,164
  • 13
  • 30
  • 38