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






CASE WHEN [condition] THEN result
    [WHEN [condition] THEN result ...]
    [ELSE result]
END

Return the result for the first condition that is true.

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
    ->        WHEN 1>0 THEN 'true'
    ->        ELSE 'false'
    -> END;
+-------------------------------------------------------------+
| CASE
       WHEN 1>0 THEN 'true'
       ELSE 'false'
END |
+-------------------------------------------------------------+
| true                                                        |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>
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