Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用存储过程批量更新表_Php_Mysql_Sql_Mysql Workbench - Fatal编程技术网

Php 使用存储过程批量更新表

Php 使用存储过程批量更新表,php,mysql,sql,mysql-workbench,Php,Mysql,Sql,Mysql Workbench,我想根据其他表的值更新表,但我的表大小非常大,为了优化查询,我想分小块更新表,但我无法这样做。这是我的剧本: 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.cu

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

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 //
调用存储过程时,出现错误:

更新和限制的使用不正确

如何避免这种情况,并使用20000条记录的批次更新完整的表

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


谢谢你的回答,但测试是星型模式中的事实表,所以我没有主数据key@Developer请参阅。它明确规定**对于多表语法,不能使用ORDER BY和LIMIT**。
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 //