gpt4 book ai didi

php - 使用存储过程批量更新表

转载 作者:行者123 更新时间:2023-11-30 00:33:36 27 4
gpt4 key购买 nike

想要根据其他表中的值更新表,但我的表大小非常大,为了优化查询,我想以小块的形式更新表,但我无法这样做。这是我的脚本:

DROP PROCEDURE IF EXISTS sp;
Delimiter //
create procedure sp()
begin
DECLARE i INT unsigned DEFAULT 0;

SET @i =1;
while i < 10 do
update
test t,
(SELECT yearweek(c.currency_date) w,
c.currency _currency, AVG(c.rate) rate
FROM currency c
GROUP BY w,_currency) src

set
t.value = t.value/src.rate,
t.currencyid = 'EUR'
where
w =(yearweek(t.created - interval 1 week) )
and t.currencyid = _currency
limit 20000;

set i = i+1;
end while;
END //

当我调用存储过程时,我收到错误:

incorrect usage of update and limit.

如何避免这种情况并使用批量 20000 记录更新完整表格

最佳答案

LIMIT 可以与 UPDATE 一起使用,但只能与 行数 一起使用

我改变了你的程序。在这里您可以使用批量更新。每次i的值增加,都会更新i的倍数,包含20000条记录

DROP PROCEDURE IF EXISTS sp;
Delimiter //
create procedure sp()
begin
DECLARE i INT unsigned DEFAULT 0;
SET @i =1;
while i < 10 do
update test t,
(SELECT yearweek(c.currency_date) w,
c.currency _currency, AVG(c.rate) rate
FROM currency c
GROUP BY w,_currency) src

set
t.value = t.value/src.rate,
t.currencyid = 'EUR'
where w =(yearweek(t.created - interval 1 week) )
and t.currencyid = _currency and t.id in (select id from test order by id limit i*20000, 20000);
set i = i+1;
end while;
END //

关于php - 使用存储过程批量更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22349598/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com