Search code examples
mysqlcomparison-operators

Is the same to use || instead OR and && instead of AND in MYSQL?


My question is the one stated in the title: "Is the same to use || instead OR and && instead of AND in MYSQL?"

I know that normally you use "AND" or "OR" as comparison operators in SQL but it (seems that) work also "&&" and "||" (like in Java/Javascript etc.) in MYSQL. Is that correct?

Thank you for the aclaration


Solution

  • As others have said, they are indeed equivalent—with the following exceptions as documented under Operator Precedence:

    The meaning of some operators depends on the SQL mode:

    • By default, || is a logical OR operator. With PIPES_AS_CONCAT enabled, || is string concatenation, with a precedence between ^ and the unary operators.

    • By default, ! has a higher precedence than NOT. With HIGH_NOT_PRECEDENCE enabled, ! and NOT have the same precedence.

    See Section 5.1.7, “Server SQL Modes”.