该死的mysql没有提供unix时间戳的专门处理函数,所以,如果遇到时间分组,而你用的又是整型unix时间戳,则只有转化为mysql的其他日期类型!
from_unixtim()将unix时间戳转为datetime等日期型!
一、年度查询
查询 本年度的数据
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')
统计一季度数据,表时间字段为: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
结果:
count( articleid ) sdate
17 0901
11 0902
5 0903
6 0904
2 0905
1 0907
12 0908
6 0909
11 0910
3 0911