本文介绍两种批量更新数据方法
数据准备
create table account
(
id int auto_increment
primary key,
balance int not null
);
insert into account(balance) values (10),(20);
1
update account t1 inner join (select 1 a,5 b union all select 2 a,15 b ) t2 set t1.balance = t2.b where t1.id = t2.a;
2
update account t set t.balance = case when id =1 then 20 when id =2 then 20 end where id in (1,2)
附:
两种方法受sql语句长度限制,和线程内存大小限制,需根据服务器情况选择批量更新条数!
稍后会附上性能测试情况