SQL /如何在sql中更新1列的多个值,具体取决于她的值和另一列的值

时间:2018-04-05 13:24:07

标签: mysql sql sql-server-2008 sql-update case

如何更新列exp: cl1 的值取决于她的值(cl1)和另一列exp的值: FL 在SQLSERVER中? 例如:

1- if cl1 = '15g0' then 20gs 
if cl1 = '22g0' then 22gs 
if cl1 = '14rx' then 14rs 

2- WHERE  FL = ' m31' OR FL= 'm28'

我用' case'但它没有工作

update table 
set
cl1 = (case when cl1 = '15g0' then 20gs end ),
cl1 = (case when cl1 = '22g0' then 22gs end ),
cl1 = (case when cl1 = '11rx' then 14rs end )

where( FL like 'm31' ) or ( FL like 'm28') 

2 个答案:

答案 0 :(得分:1)

在单个case语句中更新,还必须包含单引号

update table 
set
cl1 = case  when cl1 = '15g0' then '20gs' 
            when cl1 = '22g0' then '22gs' 
            when cl1 = '11rx' then '14rs' end 
where( FL = 'm31' ) or ( FL = 'm28')

答案 1 :(得分:-1)

在单个Update cl1语句中

case并在值周围添加单引号,如下所示:

UPDATE table 
SET cl1 = (CASE WHEN cl1 = '15g0' THEN '20gs'
                WHEN cl1 = '22g0' THEN '22gs'
                WHEN cl1 = '11rx' THEN '14rs' END)
WHERE ( FL LIKE 'm31' ) OR ( FL LIKE 'm28')