代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
MYSQL
】
MySQL 使用存储过程实现留存率
作者:
/ 发布于
2018/5/14
/
1065
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 ;
试试其它关键字
同语言下
.
MySQL按日期分组统计(按天统计,按月统计)
.
mysql 获取某个库下面所有表的所有数据(
.
查看表当前自增值
.
查看碎片较多的表
.
查看数据表较大的表
.
查看非InnoDB引擎表
.
根据某一字段值按照某一特定符转化为列数据
.
按时间段分组查询当天小时内数据
.
mysql json 使用 类型 查询 函数
.
按天数据统计
可能有用的
.
MySQL按日期分组统计(按天统计,按月统计)
.
mysql 获取某个库下面所有表的所有数据(
.
查看表当前自增值
.
查看碎片较多的表
.
查看数据表较大的表
.
查看非InnoDB引擎表
.
根据某一字段值按照某一特定符转化为列数据
.
按时间段分组查询当天小时内数据
.
mysql json 使用 类型 查询 函数
.
按天数据统计
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3