CASE in MySQL is an operator and expression, where each use is slightly different.
As a statement, CASE works just like a switch statement, and is useful in stored procedures, as shown in this example in the documentation (see above):
DELIMITER | CREATE PROCEDURE p() BEGIN DECLARE v INT DEFAULT 1; CASE v WHEN 2 THEN SELECT v; WHEN 3 THEN SELECT 0; ELSE BEGIN
However, as an expression, it can be used in articles:
SELECT * FROM employees ORDER BY CASE title WHEN "President" THEN 1 WHEN "Manager" THEN 2 ELSE 3 END, surname
In addition, both as an operator and as an expression, the first argument can be omitted, and each WHEN must accept the condition.
SELECT * FROM employees ORDER BY CASE WHEN title = "President" THEN 1 WHEN title = "Manager" THEN 2 ELSE 3 END, surname
I gave this answer because the other answer does not mention that CASE can function both expression and expression. The main difference between the two is that the form of the statement ends in END CASE , and the form of the expression ends in END only.
Svip Jan 02 '15 at 16:14 2015-01-02 16:14
source share