8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

MySQL 8.4 参考手册 / 14 函数和运算符 / 14.5 流量控制功能
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 87%

14.5 流程控制函数14.5 Flow Control Functions

表 14.7 流程控制运算符Table 14.7 Flow Control Operators

名称Name 说明Description
CASE 案例操作符Case operator
IF() 如果/否则 结构If/else construct
IFNULL() 空的 if/else 结构Null if/else construct
NULLIF() 如果 expr1 = expr2,则返回 NULLReturn NULL if expr1 = expr2

  • CASE value WHEN compare_value THEN result [WHEN compare_value THEN result ...] [ELSE result] ENDCASE value WHEN compare_value THEN result [WHEN compare_value THEN result ...] [ELSE result] END

    CASE WHEN condition THEN result [WHEN condition THEN result ...] [ELSE result] ENDCASE WHEN condition THEN result [WHEN condition THEN result ...] [ELSE result] END

    第一个 CASE 语法返回第一个 value=compare_value 比较为真的 result。第二种语法返回第一个条件为真的结果。如果没有比较或条件为真,则返回 ELSE 之后的结果,如果没有 ELSE 部分,则返回 NULL。The first CASE syntax returns the result for the first value=compare_value comparison that is true. The second syntax returns the result for the first condition that is true. If no comparison or condition is true, the result after ELSE is returned, or NULL if there is no ELSE part.

    Note

    这里描述的CASE 运算符的语法与 SQL 中的CASE 语句在第 15.6.5.1 节,“CASE 语句”中描述的用法稍有不同,用于存储程序中。CASE语句不能有ELSE NULL子句,并且以END CASE结束,而不是END。The syntax of the CASE operator described here differs slightly from that of the SQL CASE statement described in Section 15.6.5.1, “CASE Statement”, for use inside stored programs. The CASE statement cannot have an ELSE NULL clause, and it is terminated with END CASE instead of END.

    CASE 表达式结果的返回类型是所有结果值的聚合类型:The return type of a CASE expression result is the aggregated type of all result values:

    • 如果所有类型都是数字类型,则聚合类型也是数字类型:If all types are numeric, the aggregated type is also numeric:

      • 如果至少有一个参数是双精度类型,结果就是双精度类型。If at least one argument is double precision, the result is double precision.

      • 否则,如果至少有一个参数是DECIMAL,结果就是DECIMAL。Otherwise, if at least one argument is DECIMAL, the result is DECIMAL.

      • 否则,结果是整数类型(有一个例外):Otherwise, the result is an integer type (with one exception):

        • 如果所有整数类型都是有符号或都是无符号,则结果为相同的符号,并且精度为所有指定整数类型中最高的类型(即 TINYINT、SMALLINT、MEDIUMINT、INT 或 BIGINT)。If all integer types are all signed or all unsigned, the result is the same sign and the precision is the highest of all specified integer types (that is, TINYINT, SMALLINT, MEDIUMINT, INT, or BIGINT).

        • 如果存在有符号和无符号整数类型的组合,结果将是有符号的,并且精度可能更高。例如,如果类型是有符号的 INT 和无符号的 INT,结果是有符号的 BIGINT。If there is a combination of signed and unsigned integer types, the result is signed and the precision may be higher. For example, if the types are signed INT and unsigned INT, the result is signed BIGINT.

        • 例外是无符号的 BIGINT 与任何有符号整数类型组合。结果是具有足够精度且小数位为 0 的 DECIMAL。The exception is unsigned BIGINT combined with any signed integer type. The result is DECIMAL with sufficient precision and scale 0.

    • 如果所有类型都是BIT,结果就是BIT。否则,BIT 参数的处理类似于BIGINT。If all types are BIT, the result is BIT. Otherwise, BIT arguments are treated similar to BIGINT.

    • 如果所有类型都是 YEAR,结果为 YEAR。否则,YEAR 参数将类似于 INT 进行处理。If all types are YEAR, the result is YEAR. Otherwise, YEAR arguments are treated similar to INT.

    • 如果所有类型都是字符字符串(CHAR 或 VARCHAR),结果是 VARCHAR,最大长度由操作数中最长的字符长度决定。If all types are character string (CHAR or VARCHAR), the result is VARCHAR with maximum length determined by the longest character length of the operands.

    • 如果所有类型都是字符或二进制字符串,结果是 VARBINARY。If all types are character or binary string, the result is VARBINARY.

    • SET 和 ENUM 的处理方法类似于 VARCHAR;结果是 VARCHAR。SET and ENUM are treated similar to VARCHAR; the result is VARCHAR.

    • 如果所有类型为 JSON,结果为 JSON。If all types are JSON, the result is JSON.

    • 如果所有类型都是时间类型,则结果也是时间类型:If all types are temporal, the result is temporal:

    • 如果所有类型都是 GEOMETRY,结果是 GEOMETRY。If all types are GEOMETRY, the result is GEOMETRY.

    • 如果任何类型是 BLOB,结果是 BLOB。If any type is BLOB, the result is BLOB.

    • 对于所有其他类型组合,结果是 VARCHAR。For all other type combinations, the result is VARCHAR.

    • 字面 NULL 操作数在类型聚合中会被忽略。Literal NULL operands are ignored for type aggregation.

    mysql> SELECT CASE 1 WHEN 1 THEN 'one'
        ->     WHEN 2 THEN 'two' ELSE 'more' END;
            -> 'one'
    mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
            -> 'true'
    mysql> SELECT CASE BINARY 'B'
        ->     WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;
            -> NULL
    
  • IF(expr1,expr2,expr3)

    如果 expr1 为 TRUE(expr1 <> 0 且 expr1 IS NOT NULL),IF() 返回 expr2。否则,返回 expr3。If expr1 is TRUE (expr1 <> 0 and expr1 IS NOT NULL), IF() returns expr2. Otherwise, it returns expr3.

    Note

    还有一个 IF 语句,它与这里描述的 IF() 函数 不同。参见 第 15.6.5.2 节,“IF 语句”。There is also an IF statement, which differs from the IF() function described here. See Section 15.6.5.2, “IF Statement”.

    如果 expr2 或 expr3 中只有一个被显式设为 NULL,则 IF() 函数的结果类型就是非 NULL 表达式的类型。If only one of expr2 or expr3 is explicitly NULL, the result type of the IF() function is the type of the non-NULL expression.

    IF() 的默认返回类型(当它存储到临时表中时可能会有影响)计算如下:The default return type of IF() (which may matter when it is stored into a temporary table) is calculated as follows:

    • 如果 expr2 或 expr3 产生字符串,则结果为字符串。If expr2 or expr3 produce a string, the result is a string.

      如果 expr2 和 expr3 都是字符串,则结果如果任一字符串是区分大小写的,则结果也区分大小写。If expr2 and expr3 are both strings, the result is case-sensitive if either string is case-sensitive.

    • 如果 expr2 或 expr3 产生浮点数,则结果为浮点数。If expr2 or expr3 produce a floating-point value, the result is a floating-point value.

    • 如果 expr2 或 expr3 产生整数,则结果为整数。If expr2 or expr3 produce an integer, the result is an integer.

    mysql> SELECT IF(1>2,2,3);
            -> 3
    mysql> SELECT IF(1<2,'yes','no');
            -> 'yes'
    mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
            -> 'no'
    
  • IFNULL(expr1,expr2)

    如果 expr1 不是 NULL,IFNULL() 返回 expr1;否则返回 expr2。If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.

    mysql> SELECT IFNULL(1,0);
            -> 1
    mysql> SELECT IFNULL(NULL,10);
            -> 10
    mysql> SELECT IFNULL(1/0,10);
            -> 10
    mysql> SELECT IFNULL(1/0,'yes');
            -> 'yes'
    

    The default return type of IFNULL(expr1,expr2)是两种表达式中较为“通用”的一个,顺序为STRING, REAL,或INTEGER。考虑基于表达式的表的情况,或 MySQL 必须在临时表中内部存储由IFNULL()返回的值:The default return type of IFNULL(expr1,expr2) is the more “general” of the two expressions, in the order STRING, REAL, or INTEGER. Consider the case of a table based on expressions or where MySQL must internally store a value returned by IFNULL() in a temporary table:

    mysql> CREATE TABLE tmp SELECT IFNULL(1,'test') AS test;
    mysql> DESCRIBE tmp;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type         | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | test  | varbinary(4) | NO   |     |         |       |
    +-------+--------------+------+-----+---------+-------+
    

    在这个例子中,test 列的类型是VARBINARY(4)(一种字符串类型)。In this example, the type of the test column is VARBINARY(4) (a string type).

  • NULLIF(expr1,expr2)

    如果 NULL 为真,则返回 expr1 = expr2,否则返回 expr1。这与 CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END 相同。Returns NULL if expr1 = expr2 is true, otherwise returns expr1. This is the same as CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END.

    返回值与第一个参数的类型相同。The return value has the same type as the first argument.

    mysql> SELECT NULLIF(1,1);
            -> NULL
    mysql> SELECT NULLIF(1,2);
            -> 1
    
    Note

    如果参数不相等,MySQL 将对 expr1 进行两次求值。MySQL evaluates expr1 twice if the arguments are not equal.

对于这些函数中的每一个,如果第一个参数仅包含第二个参数使用的字符集和排序规则中的字符(并且它是常量),则使用后者的字符集和排序规则进行比较。系统变量的值将按相同字符集和排序规则作为列值处理。使用这些函数和系统变量的一些查询可能会因此被拒绝,并出现 排序规则混用非法 错误。在这种情况下,您应将系统变量转换为正确的字符集和排序规则。For each of these functions, if the first argument contains only characters present in the character set and collation used by the second argument (and it is constant), the latter character set and collation is used to make the comparison. System variable values are handled as column values of the same character and collation. Some queries using these functions with system variables may be rejected with Illegal mix of collations as a result. In such cases, you should cast the system variable to the correct character set and collation.