开发者

Conditional update in SQL

开发者 https://www.devze.com 2023-04-02 17:01 出处:网络
I want a conditional result in my query.开发者_运维知识库 I have a column named co1. How can I get

I want a conditional result in my query.开发者_运维知识库 I have a column named co1. How can I get

15 when c1 > 50
2  when c1 > 20
0 otherwise

If I use

CASE WHEN (co1>50) THEN 15 ELSE 0 END

But when I add the 3rd clause MySQL returns an error

CASE WHEN (co1>50) THEN 15 ELSE  WHEN (co1>20) THEN 2 ELSE 0 END

What is wrong in the second case? Thanks in advance


ELSE should only be in the last clause. Use this:

CASE WHEN (co1>50) THEN 15 WHEN (co1>20) THEN 2 ELSE 0 END


When you say ELSE - you mean it's the last condition. So it should be

CASE  
    WHEN (co1>50) THEN 15 
    WHEN (co1>20) THEN 2 
   ELSE 0 
END
0

精彩评论

暂无评论...
验证码 换一张
取 消