Mega Code Archive

 
Categories / MySQL / Function
 

CASE() Function

mysql> The CASE() function supports two slightly different formats. mysql> The first of these is shown in the following syntax: mysql> CASE WHEN <expression> THEN <result> [{WHEN <expression> THEN <result>}...] [ELSE <result>] END mysql> mysql> mysql> SELECT CASE WHEN 10*2=30 THEN '30 correct'     -> WHEN 10*2=40 THEN '40 correct'     -> ELSE 'Should be 10*2=20'     -> END; +-------------------------------------------------------------------------------------------------+ | CASE WHEN 10*2=30 THEN '30 correct' WHEN 10*2=40 THEN '40 correct' ELSE 'Should be 10*2=20' END | +-------------------------------------------------------------------------------------------------+ | Should be 10*2=20                                                                               | +-------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)