You can use control flow functions to evaluate conditions in SQL queries. This topic describes the syntax of control flow functions in AnalyticDB for MySQL and provides examples.
Sample data
In this example, a table named conditiontest is used to test the control flow functions.
Execute the following statements to create a table named conditiontest and insert data into the table:
CREATE TABLE conditiontest(a INT) DISTRIBUTED BY HASH(a);
INSERT INTO conditiontest VALUES (1),(2),(3);CASE
Syntax 1
CASE expression
WHEN value THEN result
[WHEN value THEN result ...]
[ELSE result]
END Description: This function compares the values of the
valueparameter with the value of theexpressionparameter in sequence. If the value of thevalueparameter is the same as the value of theexpressionparameter, this function returns theresultvalue of thevalueparameter. If no value of thevalueparameter is the same as the value of theexpressionparameter, this function returns theresultvalue in theELSEclause.Example:
SELECT a, CASE a WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'three' END as caseresult FROM conditiontest;Sample result:
+---+------------+ | a | caseresult | +---+------------+ | 2 | two | | 1 | one | | 3 | three | +---+------------+
Syntax 2
CASE
WHEN condition THEN result
[WHEN condition THEN result...]
[ELSE result]
ENDDescription: This function checks the values of the
conditionparameter in sequence. If the value of theconditionparameter is true, this function returns theresultvalue of theconditionparameter. If all values of theconditionparameter are false, this function returns theresultvalue in theELSEclause.Example:
SELECT a, CASE WHEN a=1 THEN 'one1' WHEN a=2 THEN 'two2' ELSE 'three3' END as caseresult FROM conditiontest;Sample result:
+---+------------+ | a | caseresult | +---+------------+ | 1 | one1 | | 3 | three3 | | 2 | two2 | +---+------------+
IF
Syntax 1
IF(condition, true_value)Description: If the value of the
conditionparameter istrue, this function returns the value of thetrue_valueparameter. Otherwise, this function returnsnull.Example:
SELECT IF((2+3)>4,5);+-------+ | _col0 | +-------+ | 5 |
Syntax 2
IF(condition, true_value, false_value)Description: If the value of the
conditionparameter istrue, this function returns the value of thetrue_valueparameter. Otherwise, this function returns the value of thefalse_valueparameter.Example:
SELECT IF((2+3)<5,5,6);+-------+ | _col0 | +-------+ | 6 |
IFNULL
IFNULL(expr1,expr2)Description: If the value of the
expr1parameter is not null, this function returns the value of theexpr1parameter. Otherwise, this function returns the value of theexpr2parameter.Examples:
Example 1:
SELECT IFNULL(NULL,2);+-------+ | _col0 | +-------+ | 2 | +-------+Example 2:
SELECT IFNULL(1,0);+-------+ | _col0 | +-------+ | 1 | +-------+
NULLIF
NULLIF(expr1,expr2)Description: If the value of the
expr1parameter is the same as the value of theexpr2parameter, this function returnsnull. Otherwise, this function returns the value of theexpr1parameter.Examples:
Example 1:
SELECT NULLIF (2,1);+-------+ | _col0 | +-------+ | 2 | +-------+Example 2:
SELECT NULLIF (2,2);+-------+ | _col0 | +-------+ | NULL | +-------+