WHEN and CASE in SQL are keywords used to check conditions and return specified values. WHEN is used to specify a condition. If the condition is true, the subsequent statement is executed; CASE is a control flow statement, which executes different statements according to the conditions that are met. The syntax is: CASE WHEN
THEN WHEN ... ELSE END. The condition can be any valid SQL expression, example: SELECT CAS
WHEN and CASE
in SQL What are WHEN and CASE?
WHEN and CASE are a set of keywords in SQL that are used to perform conditional checks and return specified values.
WHEN
CASE
How to use WHEN and CASE
The general syntax of the CASE statement is as follows:
<code class="sql">CASE WHEN <condition1> THEN <result1> WHEN <condition2> THEN <result2> ... ELSE <default_result> END</code>
The condition can be any valid SQL expression, Such as comparisons, logical operations or subqueries.
Example
The following example uses the CASE statement to return customer status:
<code class="sql">SELECT CASE WHEN status = 'A' THEN 'Active' WHEN status = 'I' THEN 'Inactive' ELSE 'Unknown' END AS customer_status FROM customers;</code>
Other tips
The above is the detailed content of What do when and case mean in sql?. For more information, please follow other related articles on the PHP Chinese website!