日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

mysql查询出去年某月_Mysql 查询某年,某季度,某月,某天搜索方法总结

發(fā)布時(shí)間:2025/3/19 74 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql查询出去年某月_Mysql 查询某年,某季度,某月,某天搜索方法总结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

假設(shè)有一張oa_item_info(項(xiàng)目信息表),其中created為項(xiàng)目創(chuàng)建時(shí)間字段

我們來(lái)進(jìn)行如下的搜索

1.查詢某年的數(shù)據(jù)

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

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

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

今年的數(shù)據(jù):

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

上一年的數(shù)據(jù):

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

date_sub()函數(shù):date_sub

2.查詢某季度的數(shù)據(jù)

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

先看一下quarter函數(shù)返回的數(shù)據(jù),第一列是quartername,第二列是created

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

并不是搜索本季度的數(shù)據(jù):

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

這條sql語(yǔ)句返回的是所有年份,當(dāng)前季度的數(shù)據(jù),比如現(xiàn)在是4月,會(huì)把所有年份4到6月的數(shù)據(jù)都檢索出來(lái)

搜索本季度的數(shù)據(jù):

加上本年的限制條件

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

3.查詢某月的數(shù)據(jù)

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

看一下返回?cái)?shù)據(jù):第一列是monthname,第二列是created

所有年份2月的數(shù)據(jù)

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

加上年份的限定條件就可以了

4.查詢某周的數(shù)據(jù)

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

看一下返回?cái)?shù)據(jù):第一列是weekname,第二列是created

返回的是一年中的第幾周

本周的數(shù)據(jù):

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的區(qū)別:

數(shù)據(jù)庫(kù)中加入兩條周數(shù)一致的日期:

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

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

看一下yearweek的返回值:

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

看一下搜索結(jié)果:

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

week把兩條年份不一樣的都搜出來(lái)了

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函數(shù)也可以

yearweek只搜出了今天的

值得注意的是,他們默認(rèn)都是從周日開(kāi)始算的,需要從周一開(kāi)始計(jì)算時(shí),需要加入第二個(gè)參數(shù)1:week(created,1)

date_format

上一周的數(shù)據(jù):

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

5.查詢某天的數(shù)據(jù)

今天的數(shù)據(jù):

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

to_days();返回從0年開(kāi)始的天數(shù);

select to_days(now())? ;

from_days();根據(jù)天數(shù),返回日期;

select from_days(737173)? ;

昨天的數(shù)據(jù):

這是很多博文的語(yǔ)句,看一下,現(xiàn)在是24號(hào),會(huì)搜出今天和昨天數(shù)據(jù)

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 ;

搜出的是昨天的:

總結(jié):

1.year(),從時(shí)間字段獲取年

2.quarter(),從時(shí)間字段獲取季度

3.month(),從時(shí)間字段獲取月

4.week(),從時(shí)間字段獲取周

5.yearweek(),從時(shí)間字段獲取年和周

6.date_sub(), 從時(shí)間字段減去指定時(shí)間間隔

7.date_format(),時(shí)間格式化

8.to_days(),返回從0年開(kāi)始的天數(shù);

9.from_days(),根據(jù)天數(shù),返回日期;

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') 這里的參數(shù)長(zhǎng)短

基本就是這些了,以后遇到相關(guān)的知識(shí)我再補(bǔ)充,這是我的第一篇真正的技術(shù)博客,喜歡的話,記得點(diǎn)個(gè)贊!

總結(jié)

以上是生活随笔為你收集整理的mysql查询出去年某月_Mysql 查询某年,某季度,某月,某天搜索方法总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。