mysql update中需要根据条件列更新写法update case

以下两条语句是否可以合并成一条:

update t9 set id=1 where   b>'2015-10-12'; 

update t9 set id=1, e='2015-01-01'  where  b='2015-10-12'; 

既然来写博客了,那答案肯定是可以的,

如下写法可以就上面的两条update语句合并成一条:

update t9 set id=1, e=(case when b='2015-10-12' then '2015-01-01' else e end)   where   b>='2015-10-12'; 

即:

当b等于'2015-10-12'时,e值设置为'2015-01-01'

当b大于'2015-10-12'时,e值保持不变。

这是一个小小技巧,有需要的朋友尽管拿去。

原文地址:https://www.cnblogs.com/zejin2008/p/4942727.html