MySQL 使用儲存過程實現留存率

2021-07-05 08:15:04 字數 3191 閱讀 6745

delimiter //

use `resourcemanage-dev`//

drop procedure if exists `stat_remain_player`//

create definer=`root`@`localhost` procedure `stat_remain_player`()

begin

-- 今天的日期

declare today date default curdate();

declare yesterday date default date_sub(today, interval 1 day);

declare days_ago_2 date default date_sub(today, interval 2 day);

declare days_ago_3 date default date_sub(today, interval 3 day);

declare days_ago_4 date default date_sub(today, interval 4 day);

declare days_ago_6 date default date_sub(today, interval 6 day);

declare days_ago_7 date default date_sub(today, interval 7 day);

declare days_ago_13 date default date_sub(today, interval 13 day);

declare days_ago_14 date default date_sub(today, interval 14 day);

declare days_ago_29 date default date_sub(today, interval 29 day);

declare days_ago_30 date default date_sub(today, interval 30 day);

-- 統計昨天dru(就是昨天一天的註冊人數)

insert into stat_remain(dru, stat_time, add_time) select count(uid) , date_sub(curdate(), interval 1 day), now() from t_ar_role where latelylogin>'2000-01-01' and date between date_sub(curdate(), interval 1 day) and curdate();

-- 修改前天的2日留存

update stat_remain set second_day = (

(select count(distinct uid) from t_ar_role where (date between days_ago_2 and yesterday) and (latelylogin between yesterday and today))

/ (select count(distinct uid) from t_ar_role where (date between days_ago_2 and yesterday))

) where stat_time = days_ago_2;

-- 修改大前天的3日留存

update stat_remain set third_day = (

select(

(select count(distinct uid) from t_ar_role where (date between days_ago_3 and days_ago_2) and (latelylogin between yesterday and today))

/ (select count(distinct uid) from t_ar_role where (date between days_ago_3 and days_ago_2))

)) where stat_time = days_ago_3;

-- 7日留存

update stat_remain set seventh_day = (

select(

(select count(distinct uid) from t_ar_role where (date between days_ago_7 and days_ago_6) and (latelylogin between yesterday and today))

/ (select count(distinct uid) from t_ar_role where (date between days_ago_7 and days_ago_6))

)) where stat_time = days_ago_7;

-- 14日留存

update stat_remain set fourteen_day = (

select(

(select count(distinct uid) from t_ar_role where (date between days_ago_14 and days_ago_13) and (latelylogin between yesterday and today))

/ (select count(distinct uid) from t_ar_role where (date between days_ago_14 and days_ago_13))

)) where stat_time = days_ago_14;

-- 30日留存

update stat_remain set thirtieth_day = (

select(

(select count(distinct uid) from t_ar_role where (date between days_ago_30 and days_ago_29) and (latelylogin between yesterday and today))

/ (select count(distinct uid) from t_ar_role where (date between days_ago_30 and days_ago_29))

)) where stat_time = days_ago_30;

end//

delimiter ;

呼叫留存率:

call stat_remain_player();

留存率的訪問過程,

mysql統計使用者七日留存儲存過程

begin declare i int declare numareaid int 10 declare currentareaid int 10 select count areaid min areaid into a,b from option area info set numareaid ...

mysql儲存登入 MYSQL儲存過程實現使用者登入

create definer root function uc session login re son json,srvjson json returnsjson language sqlnotdeterministiccontainssql sql security definer commen...

Mysql儲存過程使用

案例 create procedure sp insert graduate certificate in psid varchar 200 in certificateid int 32 in number varchar 50 in userid int 32 in starttime date...