Rechercher dans le manuel MySQL
12.3.3 Logical Operators
In SQL, all logical operators evaluate to
TRUE
, FALSE
, or
NULL
(UNKNOWN
). In MySQL,
these are implemented as 1 (TRUE
), 0
(FALSE
), and NULL
. Most of
this is common to different SQL database servers, although some
servers may return any nonzero value for
TRUE
.
MySQL evaluates any nonzero, non-NULL
value
to TRUE
. For example, the following
statements all assess to TRUE
:
- -> 1
- -> 1
- -> 1
Logical NOT. Evaluates to
1
if the operand is0
, to0
if the operand is nonzero, andNOT NULL
returnsNULL
.- -> 0
- -> 1
- -> NULL
- -> 0
- -> 1
The last example produces
1
because the expression evaluates the same way as(!1)+1
.The
!
, operator is a nonstandard MySQL extension. As of MySQL 8.0.17, this operator is deprecated and support for it will be removed in a future MySQL version. Applications should be adjusted to use the standard SQLNOT
operator.Logical AND. Evaluates to
1
if all operands are nonzero and notNULL
, to0
if one or more operands are0
, otherwiseNULL
is returned.- -> 1
- -> 0
- -> NULL
- -> 0
- -> 0
The
&&
, operator is a nonstandard MySQL extension. As of MySQL 8.0.17, this operator is deprecated and support for it will be removed in a future MySQL version. Applications should be adjusted to use the standard SQLAND
operator.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
.- -> 1
- -> 1
- -> 0
- -> NULL
- -> 1
NoteIf the
PIPES_AS_CONCAT
SQL mode is enabled,||
signifies the SQL-standard string concatenation operator (likeCONCAT()
).The
||
, operator is a nonstandard MySQL extension. As of MySQL 8.0.17, this operator is deprecated and support for it will be removed in a future MySQL version. Applications should be adjusted to use the standard SQLOR
operator. Exception: Deprecation does not apply ifPIPES_AS_CONCAT
is enabled because, in that case,||
signifies string concatentation.Logical XOR. Returns
NULL
if either operand isNULL
. For non-NULL
operands, evaluates to1
if an odd number of operands is nonzero, otherwise0
is returned.- -> 0
- -> 1
- -> NULL
- -> 1
a XOR b
is mathematically equal to(a AND (NOT b)) OR ((NOT a) and b)
.
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-logical-operators.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.