MySQL查询本周、上周、本月、上个月份数据的sql代码
程序员文章站
2024-02-19 14:23:04
查询当前这周的数据 select name,submittime from enterprise where yearweek(date_format(submittime...
查询当前这周的数据
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 下月第一天
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 下月第一天
上一篇: java虚拟机多线程进阶篇总结