Table 12.7 Flow Control Operators流程控制运算符
CASE 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] END
The first 第一个CASE syntax returns the result for the first comparison that is true. value=compare_valueCASE语法返回第一个为value=compare_valuetrue的result。The second syntax returns the result for the first condition that is true. 第二种语法返回第一个条件为true的结果。If no comparison or condition is true, the result after 如果没有比较或条件为ELSE is returned, or NULL if there is no ELSE part.true,则返回ELSE之后的结果,如果没有ELSE部分,则返回NULL。
The syntax of the 这里描述的CASE operator described here differs slightly from that of the SQL CASE statement described in Section 13.6.5.1, “CASE Statement”, for use inside stored programs. CASE运算符的语法与第13.6.5.1节,“CASE语句”中描述的SQL CASE语句的语法略有不同,后者用于存储程序内部。The CASE statement cannot have an ELSE NULL clause, and it is terminated with END CASE instead of END.
The return type of a CASE expression result is the aggregated type of all result values:CASE表达式结果的返回类型是所有结果值的聚合类型:
If all types are numeric, the aggregated type is also numeric:如果所有类型都是数字,则聚合类型也是数字:
If at least one argument is double precision, the result is double precision.如果至少有一个参数是双精度,则结果是双精度。
Otherwise, if at least one argument is 否则,如果至少有一个参数是DECIMAL, the result is DECIMAL.DECIMAL,则结果是DECIMAL。
Otherwise, the result is an integer type (with one exception):否则,结果是整数类型(只有一个例外):
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).TINYINT、SMALLINT、MEDIUMINT、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.INT和无符号的INT,则结果是带符号的BIGINT。
The exception is unsigned 异常是无符号BIGINT combined with any signed integer type. BIGINT与任何有符号整数类型的组合。The result is 结果是DECIMAL with sufficient precision and scale 0.DECIMAL的,具有足够的精度和刻度0。
If all types are 如果所有类型都是BIT, the result is BIT. BIT,则结果是BIT。Otherwise, 否则,BIT arguments are treated similar to BIGINT.BIT参数的处理类似于BIGINT。
If all types are 如果所有类型都是YEAR, the result is YEAR. YEAR,则结果是YEAR。Otherwise, 否则,YEAR arguments are treated similar to INT.YEAR参数的处理方式与INT类似。
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.CHAR或VARCHAR),则结果是VARCHAR,其最大长度由操作数的最长字符长度决定。
If all types are character or binary string, the result is 如果所有类型都是字符或二进制字符串,则结果是VARBINARY.VARBINARY。
SET and ENUM are treated similar to VARCHAR; the result is VARCHAR.SET和ENUM的处理方式类似于VARCHAR;结果是VARCHAR。
If all types are 如果所有类型都是JSON, the result is JSON.JSON,那么结果就是JSON。
If all types are temporal, the result is temporal:如果所有类型都是时态的,则结果是时态的:
If all types are 如果所有类型都是GEOMETRY, the result is GEOMETRY.GEOMETRY,则结果是GEOMETRY。
If any type is 如果任一个类型是BLOB, the result is BLOB.BLOB,则结果是BLOB。
For all other type combinations, the result is 对于所有其他类型组合,结果是VARCHAR.VARCHAR。
Literal 类型聚合将忽略文本NULL operands are ignored for type aggregation.NULL操作数。
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 is TRUE ( and expr1 <> 0), expr1 <> NULLIF() returns expr2. expr1为TRUE( and expr1 <> 0)则expr1 <> NULLIF()返回expr2。Otherwise, it returns 否则返回expr3.expr3。
There is also an 还有一个IF statement, which differs from the IF() function described here. IF语句,它与这里描述的IF()函数不同。See Section 13.6.5.2, “IF Statement”.请参阅第13.6.5.2节,“IF语句”。
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.expr2或expr3中只有一个显式为NULL,则If()函数的结果类型为非NULL表达式的类型。
The default return type of IF() (which may matter when it is stored into a temporary table) is calculated as follows:IF()的默认返回类型(当它存储到临时表中时可能很重要)计算如下:
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.expr2或expr3生成整数,则结果为整数。
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'
If 如果expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.expr1不为NULL,则IFNULL()返回expr1;否则返回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( is the more “general” of the two expressions, in the order expr1,expr2)STRING, REAL, or INTEGER. IFNULL(的默认返回类型是这两个表达式中更“一般”的一个,按expr1,expr2)STRING、REAL或INTEGER的顺序排列。Consider the case of a table based on expressions or where MySQL must internally store a value returned by 请考虑基于表达式的表或MySQL必须在临时表中内部存储IFNULL() in a temporary table:IFNULL()返回的值的情况:
mysql>CREATE TABLE tmp SELECT IFNULL(1,'test') AS test;mysql>DESCRIBE tmp;+-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | test | varbinary(4) | NO | | | | +-------+--------------+------+-----+---------+-------+
In this example, the type of the 在本例中,test column is VARBINARY(4) (a string type).test列的类型是VARBINARY(4)(字符串类型)。
Returns 如果NULL if is true, otherwise returns expr1 = expr2expr1. 为expr1 = expr2true则返回NULL,否则返回expr1。This is the same as 它等同于CASE WHEN .expr1 = expr2 THEN NULL ELSE expr1 ENDCASE 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
MySQL evaluates 如果参数不相等,则MySQL会对expr1 twice if the arguments are not equal.expr1求值两次。