Defined:
The first type:
Case Case_value while When_value then statement_list [when When_value then Statement_list] ... [ELSE Statement_list] END case
The second type:
When the case is search_condition then statement_list [when Search_condition then Statement_list] ... [ELSE Statement_list] END case
Syntax Explanation:
The Case_value after the case can be any value, or it can be a field. When_value is the value that is to be compared, if case_value and When_value are equal, the value is statement, or null if not equal (if there is else, how to show the statement after else),
In fact, the function of the whole statement is a bit like swith case;
Example 1:
Existing table Event_register (a random table, examples are not particularly suitable, mainly used to illustrate the use of)
SELECT name, Case Namewhen ' Zhang San ' then 1 when ' small plainly ' then 2ELSE 3END as AAA from Event_register where order_id = 21312529
Operation Result:
Name |
Aaa |
Wowapps |
2 |
Tom |
1 |
Little Black |
3 |
Small Black 23 |
3 |
Example 2:
SELECT name, case if name = ' Zhang San ' then 1 when name = ' small plainly ' then 2else 3END as AAA from Event_register where order_id = 21312529
Operation Result:
Name |
Aaa |
Wowapps |
2 |
Tom |
1 |
Little Black |
3 |
Small Black 23 |
3 |
The Case_value is removed, and the value of When_value becomes an expression , which is generally the same.
MySQL syntax one: When the case is detailed