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();

原创文章,作者:admin,如若转载,请注明出处:https://www.qq65hfghe5.com/tg/1910.html