mysql统计最近6天或6周或6个月的数据,没有数据自动补0

mysql查询最近6天的数据,没有数据自动补0; 以时间为基准,如果那天没数据自动补0;

 

首先利用系统表创建一个虚拟的数据表:

参考:https://blog.csdn.net/sq_com/article/details/84997563

 

SELECT (@s:=@s+1) AS _index,STR_TO_DATE(DATE(DATE_SUB(CURRENT_DATE,INTERVAL @s DAY)),'%Y-%c-%d')  AS _date
    FROM information_schema.CHARACTER_SETS,(SELECT @s:=-1) AS init WHERE @s < 5 ORDER BY _date

这样就有可以得到一张临时的数据表;information_schema.CHARACTER_SETS为系统表(设置为随便一张系统表都可以)。

mysql统计最近6天或6周或6个月的数据,没有数据自动补0_第1张图片(今天日期为2019-07-16)

然后在LEFT JOIN 你要实际查出来的数据,例如以下是我查出的数据:

SELECT 
        SUM(IFNULL(o.fare,0)) AS allFare,

        STR_TO_DATE(DATE_FORMAT(o.finish_time,'%Y-%c-%d'),'%Y-%c-%d') AS finish_date 

        FROM orders o 

        WHERE o.employee_id = 17 AND o.order_status = 2 
    
        AND STR_TO_DATE(DATE_FORMAT(o.finish_time,'%Y-%c-%d'),'%Y-%c-%d') >='2019-07-11 00:00:00' 

        AND STR_TO_DATE(DATE_FORMAT(o.finish_time,'%Y-%c-%d'),'%Y-%c-%d') <= '2019-07-16 00:00:00'

        GROUP BY finish_date

        ORDER BY finish_date 

结果:

上面两者合并起来:

SELECT tbl._date AS dateString,IFNULL(tbr.allFare,0) as balance
FROM (SELECT (@s:=@s+1) AS _index,STR_TO_DATE(DATE(DATE_SUB(CURRENT_DATE,INTERVAL @s DAY)),'%Y-%c-%d')  AS _date
                FROM information_schema.CHARACTER_SETS,(SELECT @s:=-1) AS init WHERE @s<5 ORDER BY _date) AS tbl
LEFT JOIN(SELECT 
        SUM(IFNULL(o.fare,0)) AS allFare,

        STR_TO_DATE(DATE_FORMAT(o.finish_time,'%Y-%c-%d'),'%Y-%c-%d') AS finish_date 

        FROM orders o 

        WHERE o.employee_id = 17 AND o.order_status = 2 
    
        AND STR_TO_DATE(DATE_FORMAT(o.finish_time,'%Y-%c-%d'),'%Y-%c-%d') >='2019-07-11 00:00:00' 

        AND STR_TO_DATE(DATE_FORMAT(o.finish_time,'%Y-%c-%d'),'%Y-%c-%d') <= '2019-07-16 00:00:00'

        GROUP BY finish_date

        ORDER BY finish_date 

) AS tbr ON tbl._date = tbr.finish_date  GROUP BY tbl._date;

得到的结果如下:

mysql统计最近6天或6周或6个月的数据,没有数据自动补0_第2张图片

用到的Myql函数有:

DATE_SUB() 函数:从日期减去指定的时间间隔;

DATE() 函数:返回日期或日期/时间表达式的日期部分。

STR_TO_DATE()函数:按照指定日期或时间显示格式 将字符串转换为日期或日期时间类型;

DATE_FORMAT()函数:按照指定日期或时间显示格式 输出日期或日期时间;

IFNULL() 函数:用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。

 

统计6周的也和上面一样:

SELECT tbl._date AS dateString,IFNULL(tbr.allFare,0) as balance
FROM ( SELECT (@s:=@s+1) AS _index,WEEK(DATE(DATE_SUB(CURRENT_DATE,INTERVAL @s WEEK))) AS _date
				FROM information_schema.CHARACTER_SETS,(SELECT @s:=-1) AS init WHERE @s<5 ORDER BY _date) 

AS tbl
LEFT JOIN(SELECT sum(IFNULL(o.fare,0)) allfare,WEEK(o.finish_time) AS finish_date 

		FROM orders o 

		WHERE o.employee_id = 17 AND o.order_status = 2 

		AND WEEK(o.finish_time) >= WEEK(DATE(DATE_SUB(CURRENT_DATE,INTERVAL 5 WEEK)))

		AND WEEK(o.finish_time) <= WEEK(CURRENT_DATE)
 		
	  GROUP BY finish_date ORDER BY finish_date

) AS tbr ON tbl._date = tbr.finish_date  GROUP BY tbl._date;

结果:mysql统计最近6天或6周或6个月的数据,没有数据自动补0_第3张图片其中dateString为第几周;

 

 

统计最近6个月的也一样如下:

SELECT tbl._date AS dateString,IFNULL(tbr.allFare,0) as balance
FROM ( SELECT (@s:=@s+1) AS _index,DATE_FORMAT(DATE_SUB(CURRENT_DATE,INTERVAL @s MONTH),'%Y-%m')  AS _date
				FROM information_schema.CHARACTER_SETS,(SELECT @s:=-1) AS init WHERE @s<5 ORDER BY _date) AS tbl
LEFT JOIN(SELECT sum(IFNULL(o.fare,0)) allfare,

		DATE_FORMAT(o.finish_time,'%Y-%m') AS finish_date 

		FROM orders o 

		WHERE o.employee_id = 1 AND o.order_status = 2 

		AND STR_TO_DATE(DATE_FORMAT(o.finish_time,'%Y-%m'),'%Y-%m') >= STR_TO_DATE(DATE_FORMAT(DATE_SUB(CURRENT_DATE,INTERVAL 5 MONTH),'%Y-%m'),'%Y-%m')

		AND STR_TO_DATE(DATE_FORMAT(o.finish_time,'%Y-%m'),'%Y-%m') <= STR_TO_DATE(DATE_FORMAT(CURRENT_DATE,'%Y-%m'),'%Y-%m')
 		
	  GROUP BY finish_date ORDER BY finish_date

) AS tbr ON tbl._date = tbr.finish_date  GROUP BY tbl._date;

结果:mysql统计最近6天或6周或6个月的数据,没有数据自动补0_第4张图片

 

暂时记录下!!!

你可能感兴趣的:(mysql,mybatis,springboot)