简体   繁体   English

在MYSQL的CASE语句中一次搜索多个字段值的条件

[英]search conditions for multiple field values at a time in CASE statement in MYSQL

UPDATE `Table1` SET `col3` = 
CASE `col1`
WHEN 'ABC' THEN '2'
WHEN 'BCD' THEN '3'
ELSE '0'
END 

In this, can I have condition for both col1 and col2 to set the value for col3 like if I have 在这种情况下,我是否可以有条件让col1和col2设置col3的值,就像我有

      col1 = 'ABC' AND col2 = '123'

then I want to set 然后我要设定

    col3 = 2 and col1 = 'ABC' AND col2 = '124' then set col3 = '4'

and how to set multiple column values at a time? 以及如何一次设置多个列值? Suppose here if I have col4 also then how to set both col3 and col4 values depending on the col1 and col2 values? 假设在这里如果我也有col4,那么如何根据col1和col2值设置col3和col4值?

UPDATE Table1 SET col3 = 
CASE 
WHEN col1 = 'ABC' AND col2 = '123'
THEN '2'
ELSE '0'
END
,
col4 = 
WHEN col1 = 'ABC' AND col2 = '124'
THEN 'x'
ELSE '0'
END

Updating multiple columns, and multiple conditions in when clause. 更新多列和when子句中的多个条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM