Mysql 查询某年,某季度,某月,某天搜索方法总结

假设有一张oa_item_info(项目信息表),其中created为项目建立时间字段sql

咱们来进行以下的搜索数据库

1.查询某年的数据函数

1.1 select * from oa_item_info where created like '2018-%';orm

1.2 select * from oa_item_info where left(created,4)='2018';blog

1.3 select * from oa_item_info where year(created)='2018';get

今年的数据:博客

select * from oa_item_info where year(created)=year(now());it

上一年的数据:form

select * from oa_item_info where year(created)=year(date_sub(now(),interval 1 year));date

date_sub()函数:date_sub 

 

2.查询某季度的数据

select QUARTER(created)  as quartername ,created from oa_item_info ;

先看一下quarter函数返回的数据,第一列是quartername,第二列是created

1-3月返回1,4-6月返回2,7到9月返回3,10到12月返回4

并非搜索本季度的数据:

select * from oa_item_info where QUARTER(created)=QUARTER(now());

这条sql语句返回的是全部年份,当前季度的数据,好比如今是4月,会把全部年份4到6月的数据都检索出来

 

搜索本季度的数据:

加上本年的限制条件

 select * from oa_item_info where QUARTER(created)=QUARTER(now()) and year(created)=year(now());

3.查询某月的数据

 select month(created) as monthname,created from oa_item_info;

看一下返回数据:第一列是monthname,第二列是created

全部年份2月的数据

 select * from oa_item_info where month(created)=2;

加上年份的限定条件就能够了

4.查询某周的数据

 select week(created) as weekname,created from oa_item_info ;

看一下返回数据:第一列是weekname,第二列是created

返回的是一年中的第几周

本周的数据:

 select  created   from oa_item_info where week(created)=week(now()) and year(created)=year(now());

select * from oa_item_info where YEARWEEK(date_format(created,'%Y-%m-%d')) = YEARWEEK(now()) ;

看一下week和yearweek的区别:

数据库中加入两条周数一致的日期:

select week('2017-04-20');

 

select week('2018-04-25');

看一下yearweek的返回值:

select yearweek('2018-04-25');

看一下搜索结果:

select  created   from oa_item_info where week(created)=week(now());

week把两条年份不同的都搜出来了

 select created  from oa_item_info where YEARWEEK(date_format(created,'%Y-%m-%d')) = YEARWEEK(now()) ;

select created  from oa_item_info where YEARWEEK(created) = YEARWEEK(now()) ;

不用date_format函数也能够

yearweek只搜出了今天的

值得注意的是,他们默认都是从周日开始算的,须要从周一开始计算时,须要加入第二个参数1:week(created,1)

date_format

 

上一周的数据:

select  *   from oa_item_info where YEARWEEK(date_format(created,'%Y-%m-%d')) = YEARWEEK(now())-1;

 

5.查询某天的数据

今天的数据:

 select  created from oa_item_info where to_days(created) = to_days(now());

to_days();返回从0年开始的天数;

 select to_days(now())  ;

from_days();根据天数,返回日期;

select from_days(737173)  ;

昨天的数据:

这是不少博文的语句,看一下,如今是24号,会搜出今天和昨天数据

 select  created from oa_item_info where to_days(now())-to_days(created)<=1 ;

 

 select  created from oa_item_info where to_days(now())-to_days(created)=1 ;

搜出的是昨天的:


 

总结:

1.year(),从时间字段获取年

2.quarter(),从时间字段获取季度

3.month(),从时间字段获取月

4.week(),从时间字段获取周

5.yearweek(),从时间字段获取年和周

6.date_sub(), 从时间字段减去指定时间间隔

7.date_format(),时间格式化

8.to_days(),返回从0年开始的天数;

9.from_days(),根据天数,返回日期;

SELECT * FROM `ex`.`receive` WHERE  `channeleno` =12100444 TO_DAYS(`create_time`)  = TO_DAYS(NOW()) ORDER BY `id` DESC  LIMIT 0,50;

 

select * from 表 where date_format(日期,'%Y-%m-%d')='2014-04-01' 日期
select * from 表 where date_format(日期,'%Y-%m')='2014-04' 月份
select * from 表 where date_format(日期,'%Y')='2014' 年
就是date_format(日期,'%Y-%m-%d') 这里的参数长短

基本就是这些了,之后遇到相关的知识我再补充,这是个人第一篇真正的技术博客,喜欢的话,记得点个赞!

相关文章
相关标签/搜索