Case when then, but with AND condition inside when and before then(Case when then,但在 when 和 before then 内有 AND 条件)
问题描述
在下面的查询中,我想在 CASE 的 WHEN 和 THEN 之前添加一个 AND 条件,这可能吗?
In the below query I want to add an AND condition inside the CASE's WHEN and before THEN is that possible?
例如 WHEN 'r' AND table1.name="jones" THEN 'very high'
for example WHEN 'r' AND table1.name="jones" THEN 'very high'
SELECT table1.id, table1.name,
CASE table1.event
WHEN 'r' THEN 'very high'
WHEN 't' THEN 'very low'
ELSE (SELECT table2.risk FROM table2 WHERE table2.value <= table1.value
ORDER BY table2.value DESC LIMIT 1)
END AS risk
FROM table1
ORDER BY FIELD( table1.event, 'r', 'f', 't' ), table1.value DESC
推荐答案
你可以像这样重写你的语句来完成你想要的
You can rewrite your statement like this to accomplish what you want
SELECT table1.id, table1.name,
CASE
WHEN table1.event = 'r' AND table1.name = 'jones' THEN 'very high'
WHEN table1.event = 't' AND table1.name = 'smith' THEN 'very low'
ELSE (SELECT table2.risk FROM table2 WHERE table2.value <= table1.value
ORDER BY table2.value DESC LIMIT 1)
END AS risk
FROM table1
ORDER BY FIELD( table1.event, 'r', 'f', 't' ), table1.value DESC
注意您需要删除CASE
语句之后的table1.event
.此处的文档
notice that you need to remove table1.event
after the CASE
statement.
documentation here
这篇关于Case when then,但在 when 和 before then 内有 AND 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Case when then,但在 when 和 before then 内有 AND 条件
基础教程推荐
- SQL Server 2016更改对象所有者 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01