CASE statement is used to return different values based on conditions. Syntax: CASE WHEN condition THEN result WHEN condition THEN result ... ELSE default_result END. Usage: 1. Specify a condition; 2. Return a result if the condition is true; 3. Use the ELSE clause to specify the default result when all conditions are false.
CASE statement usage in SQL
The CASE statement is used in SQL to return different values based on different conditions. value. Its syntax is as follows:
<code class="sql">CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default_result END</code>
Usage:
Example:
<code class="sql">-- 根据分数计算成绩等级 SELECT CASE WHEN score >= 90 THEN 'A' WHEN score >= 80 THEN 'B' WHEN score >= 70 THEN 'C' WHEN score >= 60 THEN 'D' ELSE 'F' END AS grade FROM students;</code>
More options:
Advantages:
Note:
The above is the detailed content of Usage of case statement in sql. For more information, please follow other related articles on the PHP Chinese website!