bitscn.com
mysql的yearweek函数以及查询本周数据
mysql 的 yearweek 是获取年份和周数的一个函数,函数形式为 yearweek(date[,mode])
例如 2010-3-14 ,礼拜天
select yearweek('2010-3-14') 返回 11
select yearweek('2010-3-14',1) 返回 10
其中第二个参数是 mode ,具体指的意思如下:
mode
first day of week
range
week 1 is the first week …
0
sunday
0-53
with a sunday in this year
1
monday
0-53
with more than 3 days this year
2
sunday
1-53
with a sunday in this year
3
monday
1-53
with more than 3 days this year
4
sunday
0-53
with more than 3 days this year
5
monday
0-53
with a monday in this year
6
sunday
1-53
with more than 3 days this year
7
monday
1-53
with a monday in this year
查询当前这周的数据
select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());
查询上周的数据
select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now())-1;
查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(now(),'%y-%m')
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询上个月的数据
select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m');
select * from `user` where date_format(pudate,'%y%m') = date_format(curdate(),'%y%m') ;
select * from user where weekofyear(from_unixtime(pudate,'%y-%m-%d')) = weekofyear(now());
select * from user where month(from_unixtime(pudate,'%y-%m-%d')) = month(now());
select * from [user] where year(from_unixtime(pudate,'%y-%m-%d')) = year(now()) and month(from_unixtime(pudate,'%y-%m-%d')) = month(now());
select * from [user] where pudate between 上月最后一天 and 下月第一天;
bitscn.com