The CASE conditional operator can act as the "IF…ELSE…THEN" logic with no need to call a subprogram.
Syntax
The CASE conditional operator has two syntaxes:
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 syntax returns the value of result when the comparison result of the first value=compare-value is True.
The second syntax returns the value of result when the first condition is True. If no comparison result or condition is True, the result after ELSE is returned. If the ELSE part is absent, NULL is returned.
Examples
obclient> SELECT CASE 'B' WHEN 'A' THEN 1 WHEN 'B' THEN 2 END;
+----------------------------------------------+
| CASE 'B' WHEN 'A' THEN 1 WHEN 'B' THEN 2 END |
+----------------------------------------------+
| 2 |
+----------------------------------------------+
1 row in set
obclient> SELECT CASE CONCAT('A','B') WHEN CONCAT('AB','') THEN 'A' WHEN 'B' THEN 'B' END;
+--------------------------------------------------------------------------+
| CASE CONCAT('A','B') WHEN CONCAT('AB','') THEN 'A' WHEN 'B' THEN 'B' END |
+--------------------------------------------------------------------------+
| A |
+--------------------------------------------------------------------------+
1 row in set
obclient> SELECT CASE WHEN 1>0 THEN 'TRUE' ELSE 'FALSE' END;
+--------------------------------------------+
| CASE WHEN 1>0 THEN 'TRUE' ELSE 'FALSE' END |
+--------------------------------------------+
| TRUE |
+--------------------------------------------+
1 row in set