CASE WHEN

Description

CASE WHEN statement is used in that evaluates a list of conditions and returns one of multiple possible result expressions.

Cases come in two formats: a simple CASE function compares an expression to a set of simple expressions to determine the result. The CASE search function evaluates a set of Boolean expressions to determine the result. Both formats support the optional ELSE argument.

Syntax

  • Syntax 1:
  1. CASE value WHEN compare_value THEN result [WHEN compare_value THEN result ...] [ELSE result] END

This CASE syntax returns the result for the first value=compare_value comparison that is true.

  • Syntax 2:
  1. CASE WHEN condition THEN result [WHEN condition THEN result ...] [ELSE result] END

This CASE syntax returns the result for the first condition that is true. If no comparison or condition is true, the result after ELSE is returned, or NULL if there is no ELSE part.

note

The CASE statement cannot have an ELSE NULL clause, and it is terminated with END CASE instead of END.

Examples

  1. mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
  2. +------------------------------------------+
  3. | case when 1 > 0 then true else false end |
  4. +------------------------------------------+
  5. | true |
  6. +------------------------------------------+
  7. 1 row in set (0.00 sec)
  1. CREATE TABLE t1 (a INT, b INT);
  2. Query OK, 0 rows affected (0.01 sec)
  3. INSERT INTO t1 VALUES (1,1),(2,1),(3,2),(4,2),(5,3),(6,3);
  4. Query OK, 6 rows affected (0.01 sec)
  5. mysql> SELECT CASE WHEN AVG(a)>=0 THEN 'Positive' ELSE 'Negative' END FROM t1 GROUP BY b;
  6. +-------------------------------------------------------+
  7. | case when avg(a) >= 0 then Positive else Negative end |
  8. +-------------------------------------------------------+
  9. | Positive |
  10. | Positive |
  11. | Positive |
  12. +-------------------------------------------------------+
  13. 3 rows in set (0.00 sec)