Casewhenthen,但在when和then之前有AND条件
在下面的查询中,我想在 CASE 的 WHEN 内和 THEN 之前添加 AND 条件,这可能吗?
例如当 '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
In the below query I want to add an AND condition inside the CASE's WHEN and before THEN is that possible?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以像这样重写您的语句来完成您想要的任务,
注意您需要在
CASE
语句之后删除table1.event
。此处的文档
You can rewrite your statement like this to accomplish what you want
notice that you need to remove
table1.event
after theCASE
statement.documentation here
任何计算结果为布尔值(true 或 false)的内容都可以进入
CASE
语句的WHEN
条件。因此,您可以将'r'
替换为:('r' AND table1.name='jones')
再考虑一下,您可能不得不丢失
CASE
之后的 table1.eventAnything that evaluates to a boolean (true or false) can go in the
WHEN
condition of aCASE
statement. So you can replace'r'
with:('r' AND table1.name='jones')
Thinking about this more, you might have to lose the
table1.event
afterCASE
从
case切换当<值>时then ...
到case when然后...
:Switch from
case <column> when <value> then ...
tocase when <condition> then ...
: