CASE with value : CASE « Control Flow Functions « MySQL Tutorial






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

Return the result where value=compare_value.

If there was no matching result value, the result after ELSE is returned, or NULL if there is no ELSE part.

mysql>
mysql> SELECT CASE 1
    ->            WHEN 1 THEN 'one'
    ->            WHEN 2 THEN 'two'
    ->            ELSE 'more'
    -> END;
+------------------------------------------------------------------------------------------------+
| CASE 1
           WHEN 1 THEN 'one'
           WHEN 2 THEN 'two'
           ELSE 'more'
END |
+------------------------------------------------------------------------------------------------+
| one                                                                                            |
+------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>








18.2.CASE
18.2.1.CASE with value
18.2.2.CASE with condition
18.2.3.CASE with case-sensitve comparison
18.2.4.Using CASE to switch the employee id
18.2.5.A SUM() function can be added to CASE statement to give us the total