Documentation Home
MySQL 9.4 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 41.2Mb
PDF (A4) - 41.3Mb
Man Pages (TGZ) - 262.8Kb
Man Pages (Zip) - 368.8Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


MySQL 9.4 Reference Manual  / ...  / Functions and Operators  / Operators  /  Logical Operators

14.4.3 Logical Operators

Table 14.5 Logical Operators

NameDescription
AND,&& Logical AND
NOT,! Negates value
OR,|| Logical OR
XOR Logical XOR

In SQL, all logical operators evaluate toTRUE,FALSE, orNULL (UNKNOWN). In MySQL, these are implemented as 1 (TRUE), 0 (FALSE), andNULL. Most of this is common to different SQL database servers, although some servers may return any nonzero value forTRUE.

MySQL evaluates any nonzero, non-NULL value toTRUE. For example, the following statements all assess toTRUE:

mysql> SELECT 10 IS TRUE;-> 1mysql> SELECT -10 IS TRUE;-> 1mysql> SELECT 'string' IS NOT NULL;-> 1
  • NOT,!

    Logical NOT. Evaluates to1 if the operand is0, to0 if the operand is nonzero, andNOT NULL returnsNULL.

    mysql> SELECT NOT 10;        -> 0mysql> SELECT NOT 0;        -> 1mysql> SELECT NOT NULL;        -> NULLmysql> SELECT ! (1+1);        -> 0mysql> SELECT ! 1+1;        -> 1

    The last example produces1 because the expression evaluates the same way as(!1)+1.

    The! operator is a nonstandard extension, and is deprecated; expect it to be removed in a future version of MySQL. Applications, where necessary, should be adjusted to use the standard SQLNOT operator instead.

  • AND,&&

    Logical AND. Evaluates to1 if all operands are nonzero and notNULL, to0 if one or more operands are0, otherwiseNULL is returned.

    mysql> SELECT 1 AND 1;        -> 1mysql> SELECT 1 AND 0;        -> 0mysql> SELECT 1 AND NULL;        -> NULLmysql> SELECT 0 AND NULL;        -> 0mysql> SELECT NULL AND 0;        -> 0

    The&&, operator is a nonstandard extension and is deprecated; expect support for it to be removed in a future version of MySQL. Applications, where necessary, should be adjusted to use the standard SQLAND operator instead.

  • OR,||

    Logical OR. When both operands are non-NULL, the result is1 if any operand is nonzero, and0 otherwise. With aNULL operand, the result is1 if the other operand is nonzero, andNULL otherwise. If both operands areNULL, the result isNULL.

    mysql> SELECT 1 OR 1;        -> 1mysql> SELECT 1 OR 0;        -> 1mysql> SELECT 0 OR 0;        -> 0mysql> SELECT 0 OR NULL;        -> NULLmysql> SELECT 1 OR NULL;        -> 1
    Note

    If thePIPES_AS_CONCAT SQL mode is enabled,|| signifies the SQL-standard string concatenation operator (likeCONCAT()).

    The||, operator is a nonstandard extension, and is deprecated; expect support for it to be removed in a future version of MySQL. Applications, where necessary, should be adjusted to use the standard SQLOR operator instead. Exception: Deprecation does not apply ifPIPES_AS_CONCAT is enabled because, in that case,|| signifies string concatenation.

  • XOR

    Logical XOR. ReturnsNULL if either operand isNULL. For non-NULL operands, evaluates to1 if an odd number of operands is nonzero, otherwise0 is returned.

    mysql> SELECT 1 XOR 1;        -> 0mysql> SELECT 1 XOR 0;        -> 1mysql> SELECT 1 XOR NULL;        -> NULLmysql> SELECT 1 XOR 1 XOR 1;        -> 1

    a XOR b is mathematically equal to(a AND (NOT b)) OR ((NOT a) and b).