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') 這裏的參數長短

基本就是這些了,之後遇到相關的知識我再補充,這是個人第一篇真正的技術博客,喜歡的話,記得點個贊!

相關文章
相關標籤/搜索