Skip to content

Instantly share code, notes, and snippets.

@yaodong
Last active August 29, 2015 14:19
Show Gist options
  • Save yaodong/ddef36775e5608c6f1b1 to your computer and use it in GitHub Desktop.
Save yaodong/ddef36775e5608c6f1b1 to your computer and use it in GitHub Desktop.
MySQL Case Expression vs Case Statement

From StackOverflow:

The statement does not evaluate to a value and can be used on its own, while the expression needs to be a part of an expression.

CASE expression:

SELECT CASE
    WHEN type = 1 THEN 'foo'
    WHEN type = 2 THEN 'bar'
    ELSE 'baz'
END AS name_for_numeric_type
FROM sometable`

CASE Statement:

CASE
    WHEN action = 'update' THEN
        UPDATE sometable SET column = value WHERE condition;
    WHEN action = 'create' THEN
        INSERT INTO sometable (column) VALUES (value);
END CASE

Related Links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment