根据条件更新查询 [英] 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}
我知道这不是有效的mysql,但这是我描述问题的最佳方法.
I know this is not valid mysql but its the best way for me to describe the problem.
推荐答案
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
这篇关于根据条件更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文