Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Query based on condition

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 fielde=3}

I know this is not valid mysql but its the best way for me to describe the problem.

like image 229
Grumpy Avatar asked Jul 13 '12 21:07

Grumpy


1 Answers

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
like image 125
juergen d Avatar answered Sep 22 '22 13:09

juergen d