Table 11.6. Flow Control Operators
Name | Description |
---|---|
CASE | Case operator |
IF() | If/else construct |
IFNULL() | Null if/else construct |
NULLIF() | Return NULL if expr1 = expr2 |
CASE
value
WHEN [compare_value
] THENresult
[WHEN [compare_value
] THENresult
...] [ELSEresult
] ENDCASE WHEN [
condition
] THENresult
[WHEN [condition
] THENresult
...] [ELSEresult
] ENDThe 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 aftervalue
=compare_value
ELSE
is returned, orNULL
if there is noELSE
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;
-> NULLThe 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.NoteThe syntax of the
CASE
expression shown here differs slightly from that of the SQLCASE
statement described in Section 12.7.6.2, “CASE
Statement”, for use inside stored programs. TheCASE
statement cannot have anELSE NULL
clause, and it is terminated withEND CASE
instead ofEND
.If
expr1
isTRUE
(
andexpr1
<> 0
) thenexpr1
<> NULLIF()
returnsexpr2
; otherwise it returnsexpr3
.IF()
returns a numeric or string value, depending on the context in which it is used.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 only one of
expr2
orexpr3
is explicitlyNULL
, the result type of theIF()
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
orexpr3
returns a stringstring expr2
orexpr3
returns a floating-point valuefloating-point expr2
orexpr3
returns an integerinteger If
expr2
andexpr3
are both strings, the result is case sensitive if either string is case sensitive.NoteThere is also an
IF
statement, which differs from theIF()
function described here. See Section 12.7.6.1, “IF
Statement”.If
expr1
is notNULL
,IFNULL()
returnsexpr1
; otherwise it returnsexpr2
.IFNULL()
returns a numeric or string value, depending on the context in which it is used.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 result value of
IFNULL(
is the more “general” of the two expressions, in the orderexpr1
,expr2
)STRING
,REAL
, orINTEGER
. Consider the case of a table based on expressions or where MySQL must internally store a value returned byIFNULL()
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 isVARBINARY(4)
.Returns
NULL
if
is true, otherwise returnsexpr1
=expr2
expr1
. This is the same asCASE WHEN
.expr1
=expr2
THEN NULL ELSEexpr1
ENDmysql>
SELECT NULLIF(1,1);
-> NULL mysql>SELECT NULLIF(1,2);
-> 1Note that MySQL evaluates
expr1
twice if the arguments are not equal.