case when 举例
时间: 2023-09-11 18:04:04 浏览: 141
CASE WHEN是SQL语言中的条件表达式,用于在查询过程中对数据进行分类和转换。下面是一个使用CASE WHEN的简单举例:
假设有一个学生成绩表,包含学生姓名和成绩两列,现在需要对成绩进行等级划分,分为优秀、良好、及格和不及格四个等级:
```
SELECT
name,
score,
CASE
WHEN score >= 90 THEN '优秀'
WHEN score >= 80 THEN '良好'
WHEN score >= 60 THEN '及格'
ELSE '不及格'
END AS grade
FROM
score_table;
```
上述语句中,CASE WHEN部分对score进行了条件判断,根据不同的分数范围返回不同的等级,最终将结果以name、score和grade三列的形式呈现出来。
相关问题
mysql case when用法举例
MySQL中,CASE WHEN语句用于根据不同的条件执行不同的操作。例如:
CASE WHEN score >= 90 THEN '优'
WHEN score >= 80 THEN '良'
WHEN score >= 70 THEN '中'
ELSE '差' END AS grade
以上代码表示,如果分数大于等于90,则输出“优”,如果分数大于等于80,则输出“良”,以此类推。如果都不符合条件,则输出“差”。
举例一下case when
MySQL中,CASE WHEN语句用于根据不同的条件执行不同的操作。例如:
CASE WHEN score >= 90 THEN '优'
WHEN score >= 80 THEN '良'
WHEN score >= 70 THEN '中'
ELSE '差' END AS grade
以上代码表示,如果分数大于等于90,则输出“优”,如果分数大于等于80,则输出“良”,以此类推。如果都不符合条件,则输出“差”。
阅读全文
相关推荐














