MYSQL 窗体汇总函数

        如果我们想要汇总当天数据,当月数据,当年数据的。如果不懂窗体函数,可能会比较费劲,那小编就说了,我用java处理同样可以达到效果啊。可问题是。明明有现成的函数,为啥要用java处理,当然同时,java会不停的判断时间范围,严重影响性能。

        当我们掌握了一下函数使用时,

①,提高工作效率

②,防止过多bug

③,提升项目性能

④, 适用于各种大屏汇总等场景

首先我们来看一下场景

表中数据每小时会有一条数据,time是每小时一次数据vachar(2)类型,

MYSQL 窗体汇总函数_第1张图片

一、如果我想汇总当天的数据呢?

SELECT
	SUM( visitors ) 
FROM
	your_table 
WHERE
	website_id = '8a87d34b8a2c0216018a2c043a220003' 
	AND DATE( create_time ) = CURDATE();

MYSQL 窗体汇总函数_第2张图片

二、如果我想汇总当月的数据呢?

SELECT
	time,
	vistros,
	SUM( vistros) OVER ( ORDER BY time ) AS total_vistros 
FROM
	(
	SELECT
		DATE_FORMAT( time, '%Y-%m-%d' ) AS time,
		sum( visitors ) vistros
	FROM
		your_table 
	WHERE
		website_id = '8a87d34b8a2c0216018a2c043a220003' 
		AND DATE_FORMAT( time, '%Y%m' )= DATE_FORMAT( CURDATE(), '%Y%m' ) 
	GROUP BY
		DATE_FORMAT( time, '%Y-%m-%d' ) 
	) t1 
ORDER BY
	time ASC

MYSQL 窗体汇总函数_第3张图片

三、如果是当年呢?


SELECT MONTH AS month,
	vistros,
	SUM( vistros ) OVER ( ORDER BY MONTH ) AS total_vistros 
FROM
	(
	SELECT
		DATE_FORMAT( time, '%Y-%m' ) AS MONTH,
		SUM( visitors ) AS vistros 
	FROM
		your_table
	WHERE
		website_id = '8a87d34b8a2c0216018a2c043a220003' 
	AND YEAR ( time )= YEAR (now()) 
	GROUP BY MONTH 
	ORDER BY MONTH DESC 
	) t1 
ORDER BY
	MONTH ASC;

MYSQL 窗体汇总函数_第4张图片

四、日期与时间查询语句:查询当年、查询本年、查询当月、查询本周

select sum(1) as years,
(select sum(1) from `表名` where  date_format( 时间字段名, '%y%m' ) = date_format( curdate( ) , '%y%m')) as months,
(select sum(1) from `表名` where to_days(时间字段名) = to_days(now()))as days
from `表名` where year(regtime)=year(now())
 
#当天  
select * from 表名 where to_days(时间字段名) = to_days(now());
 
#昨天  
select * from 表名 where to_days( now( ) ) – to_days( 时间字段名) <= 1
 
#一周
select * from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段名)
 
#近30天 select * from 表名 where date_sub(curdate(), interval 30 day) <= date(时间字段名)
 
#本月
select * from 表名 where date_format( 时间字段名, ‘%y%m’ ) = date_format( curdate( ) , ‘%y%m’ )
 
#上一月
select * from 表名 where period_diff( date_format( now( ) , ‘%y%m’ ) , date_format( 时间字段名, ‘%y%m’ ) ) =1
 
#查询本周记录
select * from ht_invoice_information where weekofyear(create_date)=weekofyear(now());
#查询上周记录
select * from ht_invoice_information where create_date>=date_add(now(),interval -(8 + weekday(now())) day) and create_date<=date_add(now(),interval -(1 + weekday(now())) day);
 
--//或者
select * from `ht_invoice_information` where weekofyear(create_date)=weekofyear(date_sub(now(),interval 1 week));
#查询本月数据
select * from ht_invoice_information where month(create_date)=month(now()) and year(create_date)=year(now());
#查询上月数据
select * from ht_invoice_information where create_date<=last_day(date_add(now(),interval -1 month)) and create_date>=date_format(concat(extract(year_month from date_add(now(),interval -1 month)),’01′),’%y-%m-%d’);
 
--//或者
select * from `ht_invoice_information` where month(create_date)=month(date_sub(now(),interval 1 month))
and year(create_date)=year(now());
 
#查询本季度数据
select * from `ht_invoice_information` where quarter(create_date)=quarter(now());
 
#查询上季度数据
select * from `ht_invoice_information` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));
 
#查询本年数据
select * from `ht_invoice_information` where year(create_date)=year(now());
 
#查询上年数据
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

参考资料:

MySQL中按月统计并逐月累加统计值的几种写法-51CTO.COM

mysql日期与时间查询语句:查询当年、查询本年、查询当月、查询本周_mysql 判断日期是某月份-CSDN博客 https://www.cnblogs.com/littlebob/p/12916389.html

你可能感兴趣的:(mysql,数据库,窗体函数)