批量更新sql

场景:批量修改用户

修改id=1,type=1的username:liuzunli1;age =11

修改id=2,type=2的username:liuzunli2;age =12

修改id=3,type=3的username:liuzunli3;age =13

 

update user

set username = (case 

when id=1 and type = 1 then "liuzunli1"

when id=2 and type = 2 then "liuzunli2"

when id=3 and type = 3 then "liuzunli3"

END),age =  (case 

when id=1 and type = 1 then 11

when id=2 and type = 2 then 21

when id=3 and type = 3 then 31

END)

where id in (1,2,3);

 

 

注意事项:where条件记得写!!不写的话,会更新全表,那么case when条件不满足的话,都设置为默认值了(或者null)

 

posted @ 2017-12-09 16:07  刘尊礼  阅读(106)  评论(0)    收藏  举报