gostgresql语法 case when then
时间: 2023-07-23 18:57:38 浏览: 144
在 PostgreSQL 中,CASE WHEN THEN 是一种条件表达式,用于根据条件执行不同的操作。
语法如下:
```
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
```
其中,condition1,condition2 等是条件表达式,result1,result2 等是对应条件为真时的结果。ELSE 子句是可选的,用于指定当没有条件匹配时的默认结果。
以下是一个示例:
```
SELECT column_name,
CASE
WHEN column_name > 10 THEN 'Greater than 10'
WHEN column_name < 10 THEN 'Less than 10'
ELSE 'Equal to 10'
END AS result
FROM table_name;
```
这个例子中,根据 column_name 的值,判断它与 10 的关系,并返回相应的结果。
希望这个回答能帮到你!如果你还有其他问题,请随时提问。
相关问题
mybatis case when then语法
在Mybatis中,可以使用case语句来进行条件判断和返回结果。Mybatis支持两种格式的case语句:简单case函数和case搜索函数。
简单case函数的语法如下:
```
case sex
when '1' then '男'
when '2' then '女'
else '未知'
end
```
这种格式适用于在case后面直接跟随一个字段或表达式,然后根据不同的取值返回不同的结果。
case搜索函数的语法如下:
```
case
when sex = '1' then '男'
when sex = '2' then '女'
else '未知'
end
```
这种格式适用于在case后面跟随多个条件表达式,然后根据条件的结果返回不同的结果。
在Mybatis中使用case语句时,需要注意语法的正确性。如果在命令行内运行没有问题,但在Mybatis中执行时报错,可能是因为语法错误或者参数传递的问题。请检查SQL语句的正确性,并确保参数的传递方式正确。
另外,Mybatis中还有其他常用的标签和语法,比如foreach标签的使用。如果你有关于Mybatis的其他问题,可以提供更多的信息,我会尽力帮助你解答。
#### 引用[.reference_title]
- *1* *3* [11.20工作总结(mybatis中SQL语法汇总)](https://blog.csdn.net/mjx715813/article/details/103161599)[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^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [mybatis case when 报错](https://blog.csdn.net/bsegebr/article/details/123659718)[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^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
case when then else end 语法
CASE WHEN THEN ELSE END 是一种 SQL 语句中的条件表达式,它可以根据条件返回不同的结果。它的基本语法如下:
```
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE resultN
END
```
其中,`condition1`、`condition2` 等是条件表达式,`result1`、`result2` 等是对应条件表达式的结果。如果所有条件都不满足,则返回 `ELSE` 后面的结果 `resultN`。
例如,下面的 SQL 语句使用 CASE WHEN THEN ELSE END 来计算员工的薪水:
```
SELECT employee_name,
CASE
WHEN salary > 5000 THEN '高薪'
WHEN salary > 3000 THEN '中薪'
ELSE '低薪'
END AS salary_level
FROM employee;
```
这个语句会根据员工的薪水计算出对应的薪水等级,并将结果作为 `salary_level` 列返回。
阅读全文
相关推荐












