Outdated Version

You are viewing an older version of this section. View current production version.

CASE

Switch statement construct. There are two species of CASE statement. The first is a collection of independent “WHEN” conditions. The first to evaluate true returns its value. The second uses only one expression or condition, and returns the first “WHEN” clause which equals the expression.

(CASE
  WHEN condition THEN val1
  WHEN condition THEN val2
  ...
  ELSE defaultval END)

Switching off a single expression:

(CASE expression
  WHEN case1 THEN val1
  WHEN case2 THEN val2
  ...
  ELSE defaultval END)

Examples

memsql> select (case
     ->  when 1=0 then 'a'
     ->  when 0=1 then 'b'
     ->  else 'c' end) as choices;
+---------+
| choices |
+---------+
| c       |
+---------+

memsql> select (case 'ohai'
     ->  when 'yo' then 'sup'
     ->  when 'hello' then 'hi'
     ->  when 'ohai' then 'bai'
     ->  else 'huh?' end) as greeting;
+----------+
| greeting |
+----------+
| bai      |
+----------+