从一表中查询出一列数据,根据这一列数据来从另外一表中进行 判断修改等操作
begin
declare done tinyint default 0;
declare uid int(11);
declare cs cursor for select userid from tbuser_red_envelope_award_log where fromactiveid="1454136362";
declare continue handler for sqlstate '02000' set done=1;
open cs;
while done<>1 do
fetch cs into uid;
select giftnum into @giftnum from tbuser_red_envelope_award_log where userid=uid and gifttemplateid=101;
if exists(select id from tbuser_bag where awardnum>=@giftnum and userid=uid and gifttemplateid=101)then
update tbuser_bag set awardnum=awardnum-@giftnum where awardnum>=@giftnum and userid=uid and gifttemplateid=101;
end if;
if exists(select id from tbuser_bag_exchange where userid=uid and excnum>=@giftnum-5 and addtime>"2016-01-30 18:00:00")then
update tbuser_bag_exchange set excnum=excnum-@giftnum,exctotalscore=exctotalscore-@giftnum where userid=uid and excnum>=@giftnum-5 and addtime>"2016-01-30 18:00:00";
end if;
delete from tbuser_bag_exchange where excnum<=0 and addtime>"2016-01-30 18:00:00";
end while;
close cs;
end
例2:
begin
declare $gameid int;
declare stop int default 0;
declare game_id cursor for select id from tbgames_box_activity;
declare continue handler for sqlstate '02000' set stop=1;
open game_id;
fetch game_id into $gameid;
while stop <> 1 do
set @today = concat(date(now())," 00:00:01");
if exists(select id from tbgames_box_activity where id = $gameid and addtime<@today and isopen=1 limit 1) then
select awardpool,smallawardboxnum,awardalloctype,awardpooltype,awardpoolremainder into @awardpool, @smallawardboxnum,@awardalloctype,@awardpooltype,@awardpoolremainder
from tbgames_box_activity where id = $gameid and addtime<@today and isopen=1 limit 1;
if @awardpooltype=0 then
if @awardalloctype=1 then
set @num=floor(1 (rand() * 3));
update tbgames_box_activity set awardpoolremainder=@awardpool @awardpoolremainder,realboxnum=@smallawardboxnum,addtime=now(),bigawardboxnum=@num where id = $gameid and addtime<@today;
else
update tbgames_box_activity set awardpoolremainder=@awardpool @awardpoolremainder,realboxnum=@smallawardboxnum,addtime=now() where id = $gameid and addtime<@today;
end if;
else
if @awardalloctype=1 then
set @num=floor(1 (rand() * 3));
update tbgames_box_activity set awardpoolremainder=@awardpool,realboxnum=@smallawardboxnum,addtime=now(),bigawardboxnum=@num where id = $gameid and addtime<@today;
else
update tbgames_box_activity set awardpoolremainder=@awardpool,realboxnum=@smallawardboxnum,addtime=now() where id = $gameid and addtime<@today;
end if;
end if;
end if;
fetch game_id into $gameid;
end while;
close game_id;
end