博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
统计连续时间
阅读量:6432 次
发布时间:2019-06-23

本文共 3924 字,大约阅读时间需要 13 分钟。

#第一步:创建存储过程
DROP PROCEDURE IF EXISTS everyCreateCount;
DELIMITER $$
CREATE PROCEDURE everyCreateCount()
BEGIN
DECLARE mintime VARCHAR(10) DEFAULT (SELECT DATE(createdate) FROM `tb_user` ORDER BY createdate ASC LIMIT 1);
DECLARE days INT DEFAULT DATEDIFF(CURDATE(),mintime);
SET @d = -1;
SELECT createdate, SUM(total) total FROM(
SELECT @d:=@d+1 d,ADDDATE(mintime,@d) createdate,(SELECT 0) total FROM customer_info WHERE @d<days
UNION ALL
SELECT (SELECT 0) d,DATE(createdate) createdate,COUNT(`userid`) total FROM `tb_user` GROUP BY DATE(createdate)
ORDER BY createdate ASC
) temp2 GROUP BY createdate
#条件添加处,1=1 后面用and添加搜索条件,当然temp2临时表结果要包含该条件
HAVING 1=1 ;
END;
$$
DELIMITER ;

CALL everyCreateCount();

 

DROP PROCEDURE IF EXISTS payInfoCreateCount;
DELIMITER $$
CREATE PROCEDURE payInfoCreateCount(IN method VARCHAR(20))
BEGIN
DECLARE mintime VARCHAR(10) DEFAULT (SELECT DATE(payDate) FROM `tb_pay_info` ORDER BY payDate ASC LIMIT 1);
DECLARE days INT DEFAULT DATEDIFF(CURDATE(),mintime);
SET @d = -1;
SELECT payDate AS createTime, SUM(total) AS total,GROUP_CONCAT(paymentmethod) paymentmethod FROM(
SELECT @d:=@d+1 d,ADDDATE(mintime,@d) payDate,(SELECT 0) total,(SELECT NULL) paymentmethod FROM customer_info WHERE @d<days
UNION ALL
SELECT (SELECT 0) d,DATE(payDate) payDate,COUNT(`orderId`) total,paymentmethod FROM `tb_pay_info` WHERE paymentmethod=method GROUP BY DATE(payDate)
ORDER BY payDate ASC
) temp2 GROUP BY payDate
#条件添加处,1=1 后面用and添加搜索条件,当然temp2临时表结果要包含该条件
HAVING 1=1 AND temp2.payDate>'2019-01-28';
END;
$$
DELIMITER ;

CALL payInfoCreateCount('directPay');

xczfPay
dgzhPay
weixinSmPcPay
directPay

SELECT `payState`,`payDate`,`paymentmethod` FROM `tb_pay_info` WHERE`payState`='FINISH' AND payDate>'2018-08-07' ;

SET @mintime=(SELECT DATE(payDate) FROM `tb_pay_info` ORDER BY payDate ASC LIMIT 1);

SET @days = (DATEDIFF(CURDATE(),@mintime));
SELECT @mintime,@days ;
SET @d = -1;
SELECT @d:=@d+1 d,ADDDATE(@mintime,@d) payDate,(SELECT 0) total FROM customer_info WHERE @d<@days

 

 

 

 

 

 

 

 

 

 

-- 定义自增变量

set @mintime=(SELECT DATE(createdate) FROM `tb_user` ORDER BY createdate asc LIMIT 1);
SET @days = DATEDIFF(CURDATE(),@mintime);
SET @d = -1;
select createdate, sum(total) total from(
SELECT @d:=@d+1 d,ADDDATE(@mintime,@d) createdate,(SELECT 0) total FROM customer_info WHERE @d<@days
union all
SELECT (select 0) d,DATE(createdate) createdate,COUNT(`userid`) total FROM `tb_user` GROUP BY DATE(createdate)
order by createdate asc
) temp2 GROUP BY createdate
#条件添加处
having temp2.createdate>'2017-06-04';

/**
比如这个 上面的createWay 的pc和wx两个条件 再根据pc和wx分别查下面的类型
根据createway查 类型是type 的数据
:
我觉着一张图做不出来....分成两张图还好些 一个pc 一个wx
我:
一个是用户,一个是订单,具体要统计什么?
:
都是单表的 根据日期统计order_info里面 createway 的pc端办理的证书新办 续期 解锁 注销 日期 而且能根据日期查出来的那种
:
和微信wx端办理的证书新办 续期 解锁 注销 日期 而且能根据日期查出来的那种

*/

 

 

 

 

 

 

 

#第一步:创建存储过程

DROP PROCEDURE IF EXISTS everyCreateCount;
DELIMITER $$
CREATE PROCEDURE everyCreateCount()
BEGIN
DECLARE minDate VARCHAR(10) DEFAULT (SELECT DATE(createdate) FROM `tb_user` ORDER BY createdate ASC LIMIT 1);
DROP TABLE IF EXISTS statisticHelp;
CREATE TABLE statisticHelp (dd VARCHAR(10));
WHILE minDate<=CURDATE() DO
INSERT INTO statisticHelp VALUES(minDate);
SET minDate=DATE_ADD(minDate,INTERVAL 1 DAY);
END WHILE ;
END;
$$
DELIMITER ;

#第二步:执行,给中间表添加初始数据

CALL everyCreateCount();

#第三步:每天定时更新中间表中的数据
SET GLOBAL event_scheduler = ON;
DROP EVENT IF EXISTS `event1`;
DELIMITER ;;
CREATE EVENT `event1` ON SCHEDULE EVERY 1 DAY STARTS DATE_ADD(DATE_ADD(CURDATE(),INTERVAL 2 HOUR),INTERVAL 30 MINUTE) ON COMPLETION PRESERVE ENABLE COMMENT '每天凌晨2:30执行一次'
DO
BEGIN
call everyCreateCount();
END
;;
DELIMITER ;

#第四步:最终结果--XML中要写的
SELECT DATE(createdate )createdate, SUM(total) total FROM (
SELECT DATE(createdate) createdate,COUNT(`userid`) total FROM `tb_user` GROUP BY DATE(createdate)
UNION ALL
SELECT DATE(`dd`) createdate,(SELECT 0) total FROM `statisticHelp`
) temp GROUP BY createdate ORDER BY createdate ASC;

 

转载于:https://www.cnblogs.com/lbky/p/10475074.html

你可能感兴趣的文章
多个VLAN中的vrrp备份组配置举例
查看>>
运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统(六)
查看>>
interlib在tomcat7.0的安装
查看>>
水晶报表在大型WEB内部管理系统里的滑铁卢
查看>>
我的友情链接
查看>>
Git学习
查看>>
trove 基于 centos7 制作 mysql5.6 镜像
查看>>
结合i节点和数据块分析linux中软链接和硬链接的区别
查看>>
Heartbeat crm的配置
查看>>
Stream
查看>>
我的友情链接
查看>>
Windows Server 2012_Install_Guide
查看>>
ISA Server搭建站点对站点×××
查看>>
我的友情链接
查看>>
超大规模数据中心:给我一个用整机柜的理由先
查看>>
执行命令取出linux中eth0的IP地址
查看>>
CRUD全栈式编程架构之控制器的设计
查看>>
python常用内建模块(五)
查看>>
你为什么有那么多时间写博客?
查看>>
Excel 中使用VBA
查看>>