在 SQL 中,所有逻辑运算符的结果都为 TRUE、FALSE 或 NULL(UNKNOWN)。在 MySQL 中,这些被实现为 1(TRUE)、0(FALSE)和 NULL。这些大多数在不同的 SQL 数据库服务器之间是通用的,尽管某些服务器可能为 TRUE 返回任何非零值。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 将任何非零、非 NULL 的值评估为 TRUE。例如,以下语句都评估为 TRUE:MySQL evaluates any nonzero, non-NULL value
to TRUE. For example, the following
statements all assess to TRUE:
mysql>SELECT 10 IS TRUE;-> 1 mysql>SELECT -10 IS TRUE;-> 1 mysql>SELECT 'string' IS NOT NULL;-> 1
逻辑 NOT。如果操作数为
0,则评估为1;如果操作数为非零,则评估为0;而NOT NULL返回NULL。Logical NOT. Evaluates to1if the operand is0, to0if the operand is nonzero, andNOT NULLreturnsNULL.mysql>
SELECT NOT 10;-> 0 mysql>SELECT NOT 0;-> 1 mysql>SELECT NOT NULL;-> NULL mysql>SELECT ! (1+1);-> 0 mysql>SELECT ! 1+1;-> 1最后一个例子生成
1,因为该表达式的评估方式与(!1)+1相同。The last example produces1because the expression evaluates the same way as(!1)+1.!运算符是一个非标准扩展,并且已被弃用;预计将在未来版本的 MySQL 中移除。必要时,应用程序应调整为使用标准 SQLNOT运算符。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 SQLNOToperator instead.逻辑 AND。当所有操作数都非零且非
NULL时,结果为1;如果有一个或多个操作数为0,结果为0,否则返回NULL。Logical AND. Evaluates to1if all operands are nonzero and notNULL, to0if one or more operands are0, otherwiseNULLis returned.mysql>
SELECT 1 AND 1;-> 1 mysql>SELECT 1 AND 0;-> 0 mysql>SELECT 1 AND NULL;-> NULL mysql>SELECT 0 AND NULL;-> 0 mysql>SELECT NULL AND 0;-> 0&&运算符是非标准扩展,已被弃用;预计在 MySQL 的未来版本中将不再支持它。必要时,应用程序应调整为使用标准 SQL 的AND运算符。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 SQLANDoperator instead.逻辑或。当两个操作数都非
NULL时,如果任一操作数非零,结果为1,否则为0。如果有一个操作数为NULL,当另一个操作数非零时,结果为1,否则结果为NULL。如果两个操作数都是NULL,结果为NULL。Logical OR. When both operands are non-NULL, the result is1if any operand is nonzero, and0otherwise. With aNULLoperand, the result is1if the other operand is nonzero, andNULLotherwise. If both operands areNULL, the result isNULL.mysql>
SELECT 1 OR 1;-> 1 mysql>SELECT 1 OR 0;-> 1 mysql>SELECT 0 OR 0;-> 0 mysql>SELECT 0 OR NULL;-> NULL mysql>SELECT 1 OR NULL;-> 1Note如果启用了
PIPES_AS_CONCATSQL 模式,||表示 SQL 标准的字符串连接运算符(类似于CONCAT())。If thePIPES_AS_CONCATSQL mode is enabled,||signifies the SQL-standard string concatenation operator (likeCONCAT()).||运算符是非标准扩展,已被弃用;预计在未来的 MySQL 版本中将不再支持它。在必要的情况下,应用程序应调整为使用标准 SQLOR运算符。例外情况:如果启用了PIPES_AS_CONCAT,则弃用规则不适用,因为在这种情况下,||表示字符串连接。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 SQLORoperator instead. Exception: Deprecation does not apply ifPIPES_AS_CONCATis enabled because, in that case,||signifies string concatenation.逻辑异或。如果任一操作数为
NULL,则返回NULL。对于非NULL的操作数,如果有奇数个操作数非零,则返回1,否则返回0。Logical XOR. ReturnsNULLif either operand isNULL. For non-NULLoperands, evaluates to1if an odd number of operands is nonzero, otherwise0is returned.mysql>
SELECT 1 XOR 1;-> 0 mysql>SELECT 1 XOR 0;-> 1 mysql>SELECT 1 XOR NULL;-> NULL mysql>SELECT 1 XOR 1 XOR 1;-> 1a XOR b在数学上等同于(a AND (NOT b)) OR ((NOT a) and b)。a XOR bis mathematically equal to(a AND (NOT b)) OR ((NOT a) and b).