8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 93%

14.4.3 逻辑运算符14.4.3 Logical Operators

表 14.5 逻辑运算符Table 14.5 Logical Operators

名称Name 说明Description
AND, && 逻辑与Logical AND
NOT, ! 否定值Negates value
OR, ||OR, || 逻辑或Logical OR
XORXOR 逻辑异或Logical XOR

在 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, !NOT, !

    逻辑 NOT。如果操作数为 0,则评估为 1;如果操作数为非零,则评估为 0;而 NOT NULL 返回 NULL。Logical NOT. Evaluates to 1 if the operand is 0, to 0 if the operand is nonzero, and NOT NULL returns 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
    

    最后一个例子生成 1,因为该表达式的评估方式与 (!1)+1 相同。The last example produces 1 because the expression evaluates the same way as (!1)+1.

    ! 运算符是一个非标准扩展,并且已被弃用;预计将在未来版本的 MySQL 中移除。必要时,应用程序应调整为使用标准 SQL NOT 运算符。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 SQL NOT operator instead.

  • AND, &&AND, &&

    逻辑 AND。当所有操作数都非零且非 NULL 时,结果为 1;如果有一个或多个操作数为 0,结果为 0,否则返回 NULL。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.

    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 SQL AND operator instead.

  • OR, ||OR, ||

    逻辑或。当两个操作数都非 NULL 时,如果任一操作数非零,结果为 1,否则为 0。如果有一个操作数为 NULL,当另一个操作数非零时,结果为 1,否则结果为 NULL。如果两个操作数都是 NULL,结果为 NULL。Logical OR. When both operands are non-NULL, the result is 1 if any operand is nonzero, and 0 otherwise. With a NULL operand, the result is 1 if the other operand is nonzero, and NULL otherwise. If both operands are NULL, the result is 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
    
    Note

    如果启用了 PIPES_AS_CONCAT SQL 模式,|| 表示 SQL 标准的字符串连接运算符(类似于 CONCAT())。If the PIPES_AS_CONCAT SQL mode is enabled, || signifies the SQL-standard string concatenation operator (like CONCAT()).

    || 运算符是非标准扩展,已被弃用;预计在未来的 MySQL 版本中将不再支持它。在必要的情况下,应用程序应调整为使用标准 SQL OR 运算符。例外情况:如果启用了 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 SQL OR operator instead. Exception: Deprecation does not apply if PIPES_AS_CONCAT is enabled because, in that case, || signifies string concatenation.

  • XORXOR

    逻辑异或。如果任一操作数为 NULL,则返回 NULL。对于非 NULL 的操作数,如果有奇数个操作数非零,则返回 1,否则返回 0。Logical XOR. Returns NULL if either operand is NULL. For non-NULL operands, evaluates to 1 if an odd number of operands is nonzero, otherwise 0 is 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;
            -> 1
    

    a XOR b 在数学上等同于 (a AND (NOT b)) OR ((NOT a) and b)。a XOR b is mathematically equal to (a AND (NOT b)) OR ((NOT a) and b).