mysql中case when then end
时间: 2023-12-26 19:27:37 浏览: 85
在MySQL中,CASE WHEN语句用于根据条件执行不同的操作。它可以在SELECT语句中使用,也可以在UPDATE或INSERT语句中使用。
以下是两种使用CASE WHEN语句的例子:
1. 简单的函数形式:
```sql
SELECT CASE score
WHEN 'a' THEN '优秀'
ELSE '良好'
END
FROM student;
```
这个例子中,根据学生的分数,如果分数是'a',则返回'优秀',否则返回'良好'。
2. 表达式形式:
```sql
SELECT CASE
WHEN score BETWEEN 90 AND 100 THEN '优秀'
ELSE '良好'
END
FROM student;
```
这个例子中,根据学生的分数范围,如果分数在90到100之间,则返回'优秀',否则返回'良好'。
相关问题
mysql的case when then end
MySQL的CASE WHEN THEN END是一种条件表达式,它可以在SELECT语句中根据条件返回不同的值。它的语法结构如下:
```
CASE expression
WHEN value1 THEN result1
WHEN value2 THEN result2
...
ELSE default_result
END
```
其中,expression是需要进行判断的表达式,value1、value2等是expression可能的取值,result1、result2等是对应的返回结果,default_result是当expression不匹配任何value时的默认返回结果。CASE WHEN THEN END可以嵌套使用,也可以和聚合函数一起使用。
mysql case end as_MySql 中 case when then else end 的用法
在 MySQL 中,CASE 表达式可以用于在 SELECT 语句中进行条件判断,类似于 if-else 语句。它的一般语法如下:
```
SELECT
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END
FROM table_name;
```
其中,condition1、condition2 等是条件表达式,result1、result2 等是对应条件为真时的结果,default_result 是所有条件均不满足时的默认结果。
CASE 表达式还可以使用简写方式:
```
SELECT
CASE column_name
WHEN value1 THEN result1
WHEN value2 THEN result2
...
ELSE default_result
END
FROM table_name;
```
在这种情况下,column_name 是要判断的列名,value1、value2 等是该列可能出现的值,result1、result2 等是对应值的结果,default_result 是该列值不在上述列表中时的默认结果。
另外,CASE 表达式还可以嵌套使用,实现更复杂的条件判断。例如:
```
SELECT
CASE
WHEN condition1 THEN
CASE
WHEN sub_condition1 THEN sub_result1
WHEN sub_condition2 THEN sub_result2
...
ELSE sub_default_result
END
WHEN condition2 THEN
CASE
WHEN sub_condition3 THEN sub_result3
WHEN sub_condition4 THEN sub_result4
...
ELSE sub_default_result
END
...
ELSE default_result
END
FROM table_name;
```
在这个例子中,CASE 表达式嵌套了两层,实现了更复杂的条件判断。
阅读全文
相关推荐














