mysql - Update Whole Table adding value from previous row -


table acc

this table acc
if edit "a" amount 100 total amount change
whole table need update...
mysql query updating whole table adding (credit) or subtracting (debit) previous total amount...

as commented @gordon linoff, can archive goal using after update trigger , loop. wrote idea below example.

delimiter //  create trigger acc_after_update after update    on acc each row  begin    declare done int default false;   declare type varchar(10);   declare total_amount default 0;   declare name varchar(10)   declare amount int;   declare cur1 cursor select name, type, amount acc;   declare continue handler not found set done = true;    open cur1;    repeat     fetch cur1 name, type, amount;      if not done       case type         when 'credit'  = total_amount + amount;         when 'debit' total_amount = total_amount - amount             end case       update acc         set amount = total_amount       name = @name --not sure syntax     end if;   until done end repeat;    close cur1;  end; //  delimiter ; 

hope helps.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -