Home >Database >Mysql Tutorial >How to use when in mysql
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:
<code class="sql">CASE expression WHEN value1 THEN result1 WHEN value2 THEN result2 ... ELSE default_result END</code>
Usage:
##Single condition:
For example:
<code class="sql">CASE WHEN salary >= 10000 THEN 'High' ELSE 'Low' END</p> <li> <p>Multiple conditions: <strong></strong></p> <ul><li>There can also be multiple A WHEN clause is used to handle different conditions. <p>For example: <br></p> <pre class="brush:php;toolbar:false"><code class="sql">CASE WHEN age < 18 THEN 'Child' WHEN age < 65 THEN 'Adult' ELSE 'Senior' END</code>
Default result:
For example:
<code class="sql">CASE WHEN gender = 'M' THEN 'Male' ELSE 'Female' END</code>
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!