首页 > 作文

mysql时间区间查询索引(教你mysql查询大于某个日期)

更新时间:2023-04-05 14:17:56 阅读: 评论:0

在很多地方都使用到了mysql的日期查询,如果不做处理,查询出来的是毫秒数,虽然就java能够处理,但是总归是要浪费时间,所以特地想总结一些关于日期查询相关的方法,供以后查询方便。

第一种:在sql中使用date_format进行格式化,得到的结果直接就是你设置的格式

lect 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

lect date_sub(curdate(),interval 0 day) ;

结果:2018-12-19王维的赠别诗

lect now();

结果:2018-12-19 22:11:53

lect curdate();

结果:2018-12-19

查询昨天:2018-12-18

lect date_sub(curdate(),interval 1 day) ;

查询明什么是学士学位天:2018-12-19

lect date_sub(curdate(),interval -1 day) ;

查询前一个小时:2018-12-19 21:19:10

lect date_sub(now(),interval 1 hour) ;

查询后一个小时:2018-12-19 21:19:10

lect date_sub(now(),interval -1 hour) ;

查询前一分钟:2018-12-19 22:19:08

lect date_sub(now(),interval 1 minute) ;

查询前一年:2017-12-19 22:19:08

lect date_sub(now(),interval 1 year) ;

第四种:查询时间段的时候,要确定是比较年月日还是比较到时分秒

lect * from test where date_format(create_time,’%y-%m-%d’) between ‘2018-07-30’ and ‘2018-07-31’;

第五种:常用的查询今天、昨天、最近几天、一个月、一个季度等数据查询

此段参考了:
/d/file/titlepic/5832897.html * from 表名 where to_days(时间字段名) = to_days(now());

昨天

lect * from 表名 where to_days( now( ) ) – to_days( 时间字段名) <= 1

近7天

lect * from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段名)

近30天

lect * from 表名 where date_sub(curdate(), interval 30 day) <= date(时间字段名)

本月

lect * from 表名 where date_format( 时间字段名, ‘%y%m’ ) = date_format( curdate( ) , ‘%y%m’ )

上一月

lect * from 表名 where period_diff( date_format( now( ) , ‘%y%m’ ) , date_format( 时间字段名, ‘%y%m’ ) ) =1

查询本季度数据

lect * from `ht_invoice_information` where quarter(create_date)=quarter(now());

查询上季度数据

lect * from `ht_invoice_information` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

查询本年数据

lect * from `ht_invoice_information` where year(create_date)=year(no首都师范大学科德学院学费w());

查询上年数据

lect * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查询当前这周的数据

lect name,submittime from enterpri where yearweek(date_format(submittime,’%y-%m-%d’)) = yearweek(now());

查询上周的数据

lect name,submittime from enterpri where yearweek(date_format(submittime,’%y-%m-%d’)) = yearweek(now())-1;

查询上个月的数据

lect name,submittime from enterpri where date_format(submittime,’%y-%m’)=date_format(date_sub(curdate(), interval 1 month),’%y-%m’)

lect * from ur where date_format(pudate,’%y%m’) = date_format(curdate大学生入党动机(),’%y%m’) ;

lect * from ur where weekofyear(from_unixtime(pudate,’%y-%m-%d’)) = weekofyear(now())

lect * from ur where month(from_unixtime(pudate,’%y-%m-%d’)) = month(now())

lect * from ur where year(from_unixtime(pudate,’%y-%m-%d’)) = year(now()) and month(from_unixtime(pudate,’%y-%m-%d’)) = month(now())

lect * from ur where pudate between 上月最后一天 and 下月第一天

查询当前月份的数据

lect name,submittime from enterpri where date_format(submittime,’%y-%m’)=date_format(now(),’%y-%m’)

查询距离当前现在6个月的数据

lect name,submittime from enterpri where submittime between date_sub(now(),interval 6 month) and now();

本文发布于:2023-04-05 14:17:54,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/eaa7c1057561b60717ae3f3bf19b0a7a.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:mysql时间区间查询索引(教你mysql查询大于某个日期).doc

本文 PDF 下载地址:mysql时间区间查询索引(教你mysql查询大于某个日期).pdf

标签:数据   时间   字段名   的是
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图