本文介紹兩種批量更新數(shù)據(jù)方法
數(shù)據(jù)準備
create table account
(
id int auto_increment
primary key,
balance int not null
);
insert into account(balance) values (10),(20);
表中數(shù)據(jù)
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;
執(zhí)行后結果
2
update account t set t.balance = case when id =1 then 20 when id =2 then 20 end where id in (1,2)
執(zhí)行后結果
附:
兩種方法受sql語句長度限制,和線程內存大小限制,需根據(jù)服務器情況選擇批量更新條數(shù)!