0
I created a stored-Procedure which updates the Drive table balances. At first I tried to use From Movimentacao
within the update
of the drive, only that returned error. I fixed this problem by making a FROM (SELECT * FROM Movimentacao) as L2
. Only this generated another problem: slowness if the table is too big, because it keeps doing SELECT * FROM Movimentacao
every hour. I tested with 10000 records and got very slow. I was breaking my head to build a query that was quick to update with select, but it’s a bit difficult.
Follow the stored-Procedure code in Mysql:
BEGIN
set @a:=0;
update Movimentacao d set d.ordem = (select @a:=@a+1) where id_conta = wconta
order by data;
UPDATE Movimentacao set saldo= (SELECT SUM(IF(operacao = 'Entrada', valor, -valor))
FROM (SELECT * FROM Movimentacao) as L2
WHERE L2.ordem <= Movimentacao.ordem and id_conta = wconta order by data,ordem)
WHERE id_conta = wconta
ORDER BY data,ordem;
END
It takes even two
Updates
on the same condition?– Carlos Andrade
The first update is to sort by date to correctly sum balances in the second update. That’s how I thought to do it on time.
– Leon4