mysql 按天分组查询
Mysql按天分组(按日分组)
一、年度查询
查询 本年度的数据
SELECT *
FROM blog_article
WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( ))
二、查询季度数据
查询数据附带季度数
SELECT ArticleId, quarter( FROM_UNIXTIME( `BlogCreateTime` ) )
FROM `blog_article`
其他的同前面部分:查询 本季度的数据
SELECT *
FROM blog_article
WHERE quarter( FROM_UNIXTIME( BlogCreateTime ) ) = quarter( curdate( ))
三、查询月度数据
本月统计(MySQL)
select * from booking where month(booking_time) =
month(curdate()) and year(booking_time) = year(curdate())
本周统计(MySQL)
select * from spf_booking where month(booking_time) =
month(curdate()) and week(booking_time) = week(curdate())
四、时间段
N天内记录
WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段) <= N
当天的记录
where date(时间字段)=date(now())
或
where to_days(时间字段) = to_days(now());
查询一周:
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);
查询一个月:
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);
查询'06-03'到'07-08'这个时间段内所有过生日的会员:
Select * From user Where
DATE_FORMAT(birthday,'%m-%d') >= '06-03' and DATE_FORMAT(birthday,'%m-%d')
<= '07-08';
统计一季度数据,表时间字段为:savetime
group by concat(date_format(savetime, ' %Y ' ), FLOOR ((date_format(savetime, ' %m ' ) + 2 ) / 3 ))
或
select YEAR (savetime) * 10 + (( MONTH (savetime) - 1 ) DIV 3 ) + 1 , count ( * )
from yourTable
group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1;
五、分组查询
1、年度分组
2、月度分组
3、先按年度分组,再按月度分组
4、按年月分组
SELECT count(ArticleId), date_format(FROM_UNIXTIME( `BlogCreateTime`),'%y%m') sdate FROM `blog_article` group by sdate