Table 12.5 Logical Operators逻辑运算符
In SQL, all logical operators evaluate to 在SQL中,所有逻辑运算符的计算结果都是TRUE, FALSE, or NULL (UNKNOWN). TRUE、FLASE或NULL(未知)。In MySQL, these are implemented as 1 (在MySQL中,它们被实现为1(TRUE), 0 (FALSE), and NULL. TRUE)、0(FLASE)和NULL。Most of this is common to different SQL database servers, although some servers may return any nonzero value for 这对于不同的SQL数据库服务器来说是很常见的,尽管有些服务器可能会返回任何非零值作为TRUE.TRUE。
MySQL evaluates any nonzero, non-MySQL将任何非零值、非NULL value to TRUE. NULL值求值为TRUE。For example, the following statements all assess to 例如,以下语句均为TRUE:TRUE:
mysql>SELECT 10 IS TRUE;-> 1 mysql>SELECT -10 IS TRUE;-> 1 mysql>SELECT 'string' IS NOT NULL;-> 1
Logical NOT. 逻辑非。Evaluates to 如果操作数为1 if the operand is 0, to 0 if the operand is nonzero, and NOT NULL returns NULL.0,则计算结果为1;如果操作数为非零,则计算结果为0,而NOT NULL则返回NULL。
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
The last example produces 上一个示例生成1 because the expression evaluates the same way as (!1)+1.1,因为表达式的计算方式与(!1)+1相同。
The 这个!, operator is a nonstandard MySQL extension. !运算符是一个非标准的MySQL扩展。As of MySQL 8.0.17, this operator is deprecated; expect it to be removed in a future version of MySQL. 从MySQL8.0.17开始,这个运算符就被弃用了;希望在MySQL的未来版本中删除它。Applications should be adjusted to use the standard SQL 应用程序应该调整为使用标准SQL的NOT operator.NOT运算符。
Logical AND. 逻辑与。Evaluates to 如果所有操作数均为非零且不为1 if all operands are nonzero and not NULL, to 0 if one or more operands are 0, otherwise NULL is returned.NULL,则计算结果为1;如果一个或多个操作数为0,则计算结果为0,否则返回NULL。
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
The &&, operator is a nonstandard MySQL extension. &&运算符是一个非标准的MySQL扩展。As of MySQL 8.0.17, this operator is deprecated; expect support for it to be removed in a future version of MySQL. 从MySQL8.0.17开始,这个操作符就被弃用了;在MySQL的未来版本中,对它的支持可能会被删除。Applications should be adjusted to use the standard SQL 应用程序应该调整为使用标准SQL的AND operator.AND运算符。
Logical OR. 逻辑或。When both operands are non-当两个操作数都非NULL, the result is 1 if any operand is nonzero, and 0 otherwise. NULL时,如果任何操作数都非零,则结果为1,否则为0。With a 对于NULL operand, the result is 1 if the other operand is nonzero, and NULL otherwise. NULL操作数,如果另一个操作数不为零,则结果为1,否则为NULL。If both operands are 如果两个操作数都为NULL, the result is NULL.NULL,则结果为NULL。
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;-> 1
If the 如果启用了PIPES_AS_CONCAT SQL mode is enabled, || signifies the SQL-standard string concatenation operator (like CONCAT()).PIPES_AS_CONCATSQL模式,||表示SQL标准字符串连接运算符(如CONCAT())。
The ||, operator is a nonstandard MySQL extension. ||运算符是一个非标准的MySQL扩展。As of MySQL 8.0.17, this operator is deprecated; expect support for it to be removed in a future version of MySQL. 从MySQL8.0.17开始,这个运算符就被弃用了;在MySQL的未来版本中,对它的支持可能会被删除。Applications should be adjusted to use the standard SQL 应用程序应该调整为使用标准SQL的OR operator. OR运算符。Exception: Deprecation does not apply if 例外情况:如果启用了PIPES_AS_CONCAT is enabled because, in that case, || signifies string concatentation.PIPES_AS_CONCAT,则不适用弃用,因为在这种情况下,||表示字符串串联。
Logical XOR. 逻辑异或。Returns 如果任一操作数为NULL if either operand is NULL. NULL,则返回NULL。For non-对于非NULL operands, evaluates to 1 if an odd number of operands is nonzero, otherwise 0 is returned.NULL操作数,如果奇数个操作数为非零,则计算结果为1,否则返回0。
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;-> 1
a XOR b is mathematically equal to (a AND (NOT b)) OR ((NOT a) and b).a XOR b在数学上等于(a AND (NOT b)) OR ((NOT a) and b)。