mysql 批量更新_mysql 批量更新的两种方法

本文介绍两种批量更新数据方法

e0f70266c810e7f287e5538f987eb7c2.png

数据准备

create table account

(

id int auto_increment

primary key,

balance int not null

);

insert into account(balance) values (10),(20);

36d6fd74b03c92b29e6a1d55bb54d129.png

表中数据

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;

7b70a971b2aeb1fec42b6cd7eba44836.png

执行后结果

2

update account t set t.balance = case when id =1 then 20 when id =2 then 20 end where id in (1,2)

baeb8ef41016e1d1503ee4a81519331c.png

执行后结果

附:

两种方法受sql语句长度限制,和线程内存大小限制,需根据服务器情况选择批量更新条数!

稍后会附上性能测试情况

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值