Casewhenthen,但在when和then之前有AND条件

发布于 2024-12-02 13:52:14 字数 471 浏览 2 评论 0原文

在下面的查询中,我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶茶白久 2024-12-09 13:52:14

您可以像这样重写您的语句来完成您想要的任务,

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
此处的文档

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

notice that you need to remove table1.event after the CASE statement.
documentation here

2024-12-09 13:52:14

任何计算结果为布尔值(true 或 false)的内容都可以进入 CASE 语句的 WHEN 条件。因此,您可以将 'r' 替换为:

('r' AND table1.name='jones')

再考虑一下,您可能不得不丢失 CASE 之后的 table1.event

SELECT table1.id, table1.name,
    CASE 
        WHEN (table1.event = 'r' AND table1.name='Jones') THEN 'very high'
        WHEN table1.event = '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

Anything that evaluates to a boolean (true or false) can go in the WHEN condition of a CASE statement. So you can replace 'r' with:

('r' AND table1.name='jones')

Thinking about this more, you might have to lose the table1.event after CASE

SELECT table1.id, table1.name,
    CASE 
        WHEN (table1.event = 'r' AND table1.name='Jones') THEN 'very high'
        WHEN table1.event = '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
习惯成性 2024-12-09 13:52:14

case切换当<值>时then ...case when然后...

CASE 
WHEN table1.event = 'r' AND table1.active = 1 THEN 'very high'
...

Switch from case <column> when <value> then ... to case when <condition> then ...:

CASE 
WHEN table1.event = 'r' AND table1.active = 1 THEN 'very high'
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文