The case when statement in the MySQL database.
A case when statement that evaluates a list of conditions and returns one of several possible result expressions.
Case has two formats:
The simple case function compares an expression to a set of simple expressions to determine the result.
The case search function calculates a set of Boolean expressions to determine the result.
The optional ELSE parameter is supported in both formats.
Grammar
Simple Case function:
Case input_expression
When when_expression then result_expression
[... n]
[
ELSE else_result_expression
END
such as: SELECT (case type when 1 Tnen ' type 1 ' when 2 Then ' Type 2 ' END) as TypeName from Cate;
Case Search Function:
Case
When Boolean_expression then result_expression
[... n]
[
ELSE else_result_expression
END
such as: SELECT (case when type=1 Tnen ' type 1 ' when the type=2 then ' Type 2 ' END) as TypeName from Cate;
MySQL case-When statement