sql case when then
时间: 2023-08-28 20:18:06 浏览: 83
sql 中 case when 语法使用方法
在SQL中,CASE语句用于根据条件返回不同的结果表达式。CASE语句的语法如下:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
在这个语法中,condition1、condition2等是条件,result1、result2等是对应条件为真时返回的结果表达式。ELSE子句是可选的,用于指定当所有条件都不满足时的默认结果表达式。 [2 [3]
例如,在查询学生成绩时,可以使用CASE语句将分数转换为对应的等级:
SELECT student_name,
CASE
WHEN score >= 90 THEN '优秀'
WHEN score >= 80 THEN '良好'
WHEN score >= 70 THEN '中等'
WHEN score >= 60 THEN '及格'
ELSE '不及格'
END AS grade
FROM student_scores;
这个查询会根据学生的分数给出相应的等级,如果没有满足任何条件,就返回不及格。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [SQL中case when then用法](https://blog.csdn.net/qq_21036939/article/details/90176929)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [SQL之case when then用法简介](https://blog.csdn.net/rr19941112/article/details/121977545)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文