Spec-Zone .ru
спецификации, руководства, описания, API
|
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 version returns the result
where
.
The second version returns the result for the first condition that is true. If there was no matching
result value, the result after value
=compare_value
ELSE
is returned, or NULL
if there is no ELSE
part.
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
The return type of a CASE
expression is the compatible aggregated type of all return
values, but also depends on the context in which it is used. If used in a string context, the result
is returned as a string. If used in a numeric context, the result is returned as a decimal, real, or
integer value.
The syntax of the CASE
expression shown here
differs slightly from that of the SQL CASE
statement described in Section
13.6.5.1, "CASE
Syntax", for use inside stored programs.
The CASE
statement cannot have an ELSE
NULL
clause, and it is terminated with END CASE
instead
of END
.
If expr1
is TRUE
(
and expr1
<> 0
) then expr1
<> NULLIF()
returns expr2
; otherwise it returns expr3
.
IF()
returns a
numeric or string value, depending on the context in which it is used.
mysql>SELECT IF(1>2,2,3);
-> 3mysql>SELECT IF(1<2,'yes','no');
-> 'yes'mysql>SELECT IF(STRCMP('test','test1'),'no','yes');
-> 'no'
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.
The default return type of IF()
(which may matter when it is stored into a temporary table) is calculated as follows.
Expression | Return Value |
---|---|
expr2 or expr3 returns a string
|
string |
expr2 or expr3 returns a floating-point value
|
floating-point |
expr2 or expr3 returns an integer
|
integer |
If expr2
and expr3
are both strings, the result is case sensitive if
either string is case sensitive.
There is also an IF
statement, which
differs from the IF()
function
described here. See Section
13.6.5.2, "IF
Syntax".
If expr1
is not NULL
, IFNULL()
returns expr1
; otherwise it returns expr2
. IFNULL()
returns a numeric or string value, depending on the
context in which it is used.
mysql>SELECT IFNULL(1,0);
-> 1mysql>SELECT IFNULL(NULL,10);
-> 10mysql>SELECT IFNULL(1/0,10);
-> 10mysql>SELECT IFNULL(1/0,'yes');
-> 'yes'
The default result value of IFNULL(
is the more "general" of the two expressions, in the order expr1
,expr2
)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 | | | |+-------+--------------+------+-----+---------+-------+
In this example, the type of the test
column is VARBINARY(4)
.
Returns NULL
if
is true, otherwise returns expr1
= expr2
expr1
. This is the same as CASE WHEN
. expr1
= expr2
THEN NULL ELSE expr1
END
mysql>SELECT NULLIF(1,1);
-> NULLmysql>SELECT NULLIF(1,2);
-> 1
Note that MySQL evaluates expr1
twice if the arguments are
not equal.