用于替换多个值的SQL语句

时间:2014-11-06 15:12:47

标签: sql

我有一个名为paint的表,其中名为color的列中有两个值,'Black''White'

我需要一个SQL语句,可以用'Black'替换值'Green',并在整列中用'White'替换'Red'

对此的任何帮助都将非常感激。

2 个答案:

答案 0 :(得分:0)

select case 
when color = 'Black' 
then 'Green' 
else 
    case 
    when color = 'White' 
    then 'Red' 
    else color 
    end
end as [Color]
from paint

答案 1 :(得分:0)

您更新所有行 颜色为黑色或白色。在 case 中,颜色为黑色,您将其设为绿色其他为红色。转换为SQL:

update paint
set color = case when 'Black' then 'Green' else 'Red' end
where color in ('Black', 'White');

嗯,我承认,翻译的内容不多: - )