The WHEN clause in MySQL is used to specify the actions to be performed when specific conditions are met. It supports: Single condition check, for example: CASE WHEN salary >= 10000 THEN 'High'. Multiple condition processing, for example: CASE WHEN age < 18 THEN 'Child' WHEN age < 65 THEN 'Adult'. Default result, for example: CASE WHEN gender = 'M' THEN 'Male' ELSE 'Female'. The WHEN clause provides concise syntax, multi-condition processing, and default results.
WHEN usage in MySQL
Overview:
The WHEN clause is CASE A branch in an expression that specifies an action to perform when a specific condition is met.
Syntax:
CASE expression WHEN value1 THEN result1 WHEN value2 THEN result2 ... ELSE default_result END
Usage:
##Single condition:
For example:
CASE WHEN salary >= 10000 THEN 'High' ELSE 'Low' END
Multiple conditions:
For example:
CASE WHEN age < 18 THEN 'Child' WHEN age < 65 THEN 'Adult' ELSE 'Senior' END
Default result:
For example:
CASE WHEN gender = 'M' THEN 'Male' ELSE 'Female' END
Advantages:
Limitations:
The above is the detailed content of How to use when in mysql. For more information, please follow other related articles on the PHP Chinese website!