Update Query based on condition

前端 未结 2 1978
谎友^
谎友^ 2021-01-18 09:40

I would like to do the following. Update a field based on the value of another field like

update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 field         


        
2条回答
  •  臣服心动
    2021-01-18 10:07

    update table set
    b = case when a = 1 then 2 else b end,
    c = case when a = 1 then 3 else c end,
    d = case when a = 1 then d else 2 end,
    e = case when a = 1 then e else 3 end
    

    edit

    according to your comment try this:

    update table set
    datefield_a = case when field_a = 1 then now() else datefield_a end,
    datefield_b = case when field_a <> 1 then now() else datefield_b end
    

提交回复
热议问题