mysql时间区间查询索引(教你mysql查询大于某个日期)
在很多地方都使用到了mysql的日期查询,如果不做处理,查询出来的是毫秒数,虽然就java能够处理,但是总归是要浪费时间,所以特地想总结一些关于日期查询相关的方法,供以后查询方便。
第一种:在sql中使用date_format进行格式化,得到的结果直接就是你设置的格式
select date_format(t.create_time,’%y-%c-%d %h:%i:%s’) ‘date’ from e_mail_accept t
得到的是:2018-12-08 16:36:47
第二种:如果是使用的ssm项目,则可以通过在对应的pojo上面加上@jsonformat注解,即可得到想要的结果
/** 创建时间 */
@jsonformat(locale=”zh”, timezone=”gmt+8″, pattern=”yyyy-mm-dd hh:mm:ss”)
@tablefield(value=”create_time”)
private date createtime;
此方法需要在maven加上如下代码
<dependency>
<groupid>com.fasterxml.jackson.core</groupid>
<artifactid>jackson-databind</artifactid>
<version>2.9.2</version>
</dependency>
第三种:查询当前日期常用的sql
select date_sub(curdate(),interval 0 day) ;
结果:2018-12-19
select now();
结果:2018-12-19 22:11:53
select curdate();
结果:2018-12-19
查询昨天:2018-12-18
select date_sub(curdate(),interval 1 day) ;
查询明天:2018-12-19
select date_sub(curdate(),interval -1 day) ;
查询前一个小时:2018-12-19 21:19:10
select date_sub(now(),interval 1 hour) ;
查询后一个小时:2018-12-19 21:19:10
select date_sub(now(),interval -1 hour) ;
查询前一分钟:2018-12-19 22:19:08
select date_sub(now(),interval 1 minute) ;
查询前一年:2017-12-19 22:19:08
select date_sub(now(),interval 1 year) ;
第四种:查询时间段的时候,要确定是比较年月日还是比较到时分秒
select * from test where date_format(create_time,’%y-%m-%d’) between ‘2018-07-30’ and ‘2018-07-31’;
第五种:常用的查询今天、昨天、最近几天、一个月、一个季度等数据查询
此段参考了:
https://www.cnblogs.com/benefitworld/p/5832897.html
今天
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天
select * from 表名 where to_days( now( ) ) – to_days( 时间字段名) <= 1
近7天
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 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));
查询当前这周的数据
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(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 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();