drop table if exists ttt;
create temporary table ttt(
id int auto_increment,
username varchar(50),
prepay int,
leftprepay int not null default 0,
totaluse int not null default 0,
tempcol varchar(50),
realuseprepay int not null default 0,
PRIMARY key(id)) DEFAULT CHARSET=utf8;
insert into ttt(username,prepay,totaluse)
select 1,3,1
union all select 1,2,1
union all select 1,1,1
union all select 2,3,8
union all select 2,5,8;
select from ttt;
set @leftmoney =0;
set @username = ‘’;
update ttt set leftprepay = (@leftmoney:= if(username=@username ,@leftmoney,totaluse) - prepay),
tempcol = (@username:= if(username=@username,@username,username)),
realuseprepay=if(@leftmoney > 0 ,prepay,@leftmoney+prepay)
order by username desc;
select from ttt;
#######下面递增
update paydetail d
set balance = (@balance := @balance + debitmoney - CreditMoney)
where d.rid > _rid;