SQL 中的 case when
时间: 2023-04-09 11:04:43 浏览: 149
case when 是 SQL 中的条件语句,用于在查询中根据条件返回不同的结果。它的语法如下:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END
其中,condition1、condition2 等是条件表达式,result1、result2 等是对应条件为真时返回的结果,default_result 是当所有条件都不满足时返回的默认结果。
相关问题
sql中case when
在SQL中,CASE WHEN语句用于根据满足指定条件时执行不同的操作。它的语法如下:
```
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
```
这里的condition1,condition2等是要满足的条件,result1,result2等是根据条件的结果。
下面是一个示例,演示如何在SQL中使用CASE WHEN语句:
假设我们有一个名为employees的表,其中包含员工的姓名和薪水。如果员工的薪水超过10000,则将其职级设置为高级,否则设置为普通。
```sql
SELECT
name,
salary,
CASE
WHEN salary > 10000 THEN '高级'
ELSE '普通'
END AS 职级
FROM employees;
```
输出将包含姓名、薪水和职级三列,其中职级根据薪水是否超过10000进行判断。
sql 中case when
在 SQL 中,CASE WHEN 是一个条件表达式,用于根据特定条件返回不同的结果。它的语法如下:
```sql
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
```
这个表达式首先评估条件1,如果满足条件1,则返回结果1;如果不满足条件1,则继续评估条件2,如果满足条件2,则返回结果2;以此类推。如果没有任何条件满足,则返回 ELSE 后面指定的结果。
CASE WHEN 可以嵌套在 SELECT 语句中,也可以作为其他 SQL 语句的一部分使用。它非常实用,可以根据不同的条件返回不同的结果。
阅读全文
相关推荐













