作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在下面的查询中,我想在 CASE 的 WHEN 和 THEN 之前添加一个 AND 条件是否可行?
例如 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
最佳答案
你可以像这样重写你的语句来完成你想要的
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
。 documentation here
关于mysql - 案例 when then,但在 when 和 before then 内有 AND 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7294890/
我是一名优秀的程序员,十分优秀!