case when where 条件
时间: 2023-08-28 19:23:19 浏览: 98
CASE WHEN语句可以在WHERE条件中作为条件之一或在SELECT语句中作为结果。在WHERE条件中,CASE WHEN语句的结果和指定的值进行比较,如果相等则表示条件满足,即进行过滤。在SELECT语句中,根据条件的不同返回不同的结果,并将结果命名为指定的名称。此外,也可以通过多个CASE WHEN语句在WHERE条件中组合使用来实现更复杂的条件过滤。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [sql示例:case when作为where 条件](https://blog.csdn.net/weixin_44060488/article/details/131000449)[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: 100%"]
[ .reference_list ]
相关问题
case when where
CASE WHEN语句可以在WHERE条件中作为条件之一,也可以在SELECT语句中作为结果。在WHERE条件中,CASE WHEN语句的结果和1进行比较,如果相等则表示条件满足,即进行过滤。在SELECT语句中,CASE WHEN语句可以根据条件返回不同的结果。下面是两个例子:
1.在WHERE条件中使用CASE WHEN语句作为条件之一
```sql
SELECT * FROM table_name
WHERE CASE WHEN condition_1 THEN 1
WHEN condition_2 THEN 1
ELSE 0
END = 1
AND condition_3 = 'xxx';
```
其中,condition_1和condition_2表示需要满足的条件,如果满足其中之一,则将CASE WHEN语句的结果设为1,否则设为0。如果CASE WHEN语句的结果等于1,并且condition_3等于'xxx',则将该行数据返回。
2.在SELECT语句中使用CASE WHEN语句作为结果
```sql
SELECT column_1, column_2,
CASE WHEN condition_1 THEN 'A'
WHEN condition_2 THEN 'B'
ELSE 'C'
END AS result
FROM table_name
WHERE condition_3 = 'xxx';
```
其中,condition_1和condition_2表示需要满足的条件,如果满足其中之一,则将CASE WHEN语句的结果设为'A'或'B',否则设为'C'。将结果命名为result并返回。
mysql case when 放在where条件后
当你想要在MySQL的WHERE条件之后使用CASE WHEN语句时,你可以使用以下语法:
```sql
SELECT column1, column2, ...
FROM table_name
WHERE condition
AND (CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END);
```
在上面的语法中,你可以在CASE WHEN语句中添加任意数量的条件和结果。如果条件满足,它将返回相应的结果。如果没有条件满足,它将返回默认结果。注意,CASE WHEN语句必须放在圆括号中,以便与其他WHERE条件正确结合使用。
阅读全文
相关推荐
















