Mega Code Archive

 
Categories / MySQL Tutorial / Control Flow Functions
 

CASE with value

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>