DELIMITER $
-- 统计留存率
DROP PROCEDURE IF EXISTS stat_remain_player$
CREATE PROCEDURE stat_remain_player()
BEGIN
declare i int;
SET i = 1;
insert into stat_remain(dru,stat_time,add_time) select count(a_qid),date_sub(curdate(),interval 1 day),now() from tb_fish where datediff(now(),add_time)=1;
while i<30 do
if (i=1) or (i=2) or (i=6) or (i=29) then
if exists(select * from stat_remain where datediff(curdate(),stat_time) =i) then
update stat_remain set second_day=(select (select count(distinct player_id) from log_login where datediff(curdate(),register_time) =i and datediff(date_add(curdate(),interval 1-i day),last_login_time)=0)/(select count(distinct player_id) from log_login where datediff(curdate(),register_time) =i))*100 where datediff(curdate(),stat_time) =i;
update stat_remain set third_day=(select (select count(distinct player_id) from log_login where datediff(curdate(),register_time) =i and datediff(date_add(curdate(),interval 2-i day),last_login_time)=0)/(select count(distinct player_id) from log_login where datediff(curdate(),register_time) =i))*100 where datediff(curdate(),stat_time) =i;
update stat_remain set seventh_day=(select (select count(distinct player_id) from log_login where datediff(curdate(),register_time) =i and datediff(date_add(curdate(),interval 6-i day),last_login_time)=0)/(select count(distinct player_id) from log_login where datediff(curdate(),register_time) =i))*100 where datediff(curdate(),stat_time) =i;
update stat_remain set thirtieth_day=(select (select count(distinct player_id) from log_login where datediff(curdate(),register_time) =i and datediff(date_add(curdate(),interval 29-i day),last_login_time)=0)/(select count(distinct player_id) from log_login where datediff(curdate(),register_time) =i))*100 where datediff(curdate(),stat_time) =i;
end if;
end if;
SET i = i + 1;
end while;
END
$
DELIMITER ;