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語句返回的是所有年份,當前季度的數(shù)據(jù)棋枕,比如現(xiàn)在是4月白修,會把所有年份4到6月的數(shù)據(jù)都檢索出來
搜索本季度的數(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;
看一下返回數(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 ;
看一下返回數(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ù)庫中加入兩條周數(shù)一致的日期:
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函數(shù)也可以
yearweek只搜出了今天的
?
值得注意的是,他們默認都是從周日開始算的窥浪,需要從周一開始計算時祖很,需要加入第二個參數(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年開始的天數(shù)漾脂;
select to_days(now()) ;
?
from_days();根據(jù)天數(shù)假颇,返回日期;
select from_days(737173) ;
?
昨天的數(shù)據(jù):
這是很多博文的語句符相,看一下拆融,現(xiàn)在是24號,會搜出今天和昨天數(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 ;
搜出的是昨天的:
?
總結:
1.year()啊终,從時間字段獲取年
2.quarter()镜豹,從時間字段獲取季度
3.month(),從時間字段獲取月
4.week()蓝牲,從時間字段獲取周
5.yearweek()趟脂,從時間字段獲取年和周
6.date_sub(), 從時間字段減去指定時間間隔
7.date_format(),時間格式化
8.to_days()例衍,返回從0年開始的天數(shù)昔期;
9.from_days(),根據(jù)天數(shù)佛玄,返回日期硼一;
1、DATE() 函數(shù):返回日期或日期時間表達式的日期部分梦抢;
2般贼、str_to_date()函數(shù):按照指定日期或時間顯示格式 將字符串轉換為日期或日期時間類型;
3奥吩、date_format()函數(shù):按照指定日期或時間顯示格式 輸出日期或日期時間哼蛆;
1、date(datestring)
datestring是合法的日期表達式
如:
SELECT date('2017-02-09 15:25:46.635')
FROM dual;
-->'2017-02-09'
2霞赫、date_format(datestring腮介,format)
datestring參數(shù)是合法的日期。format 規(guī)定日期/時間的輸出格式端衰。
如:
SELECT STR_TO_DATE('2017-02-09 15:25:46.635','%Y-%m-%d')
FROM dual;
-->'2017-02-09'
SELECT STR_TO_DATE('2017-02-09 15:25:46','%Y-%m-%d %H:%i:%s')
FROM DUAL;
-->'2017-02-09 15:25:46'
SELECT STR_TO_DATE('2017-02-09 15:25','%Y-%m-%d %k:%i')
FROM DUAL;
-->'2017-02-09 15:25:00'
3叠洗、date_format(datestring,format)
datestring參數(shù)是合法的日期旅东。format 規(guī)定日期/時間的輸出格式惕味。
如:
當前時間按月-日 時:分:秒顯示:
SELECT DATE_FORMAT(NOW(),'%m-%d %h:%i %p')
FROM dual;
-->'02-09 06:00 PM'
當前時間按 年-月-日 時:分:秒 AM/PM顯示:
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %h:%i:%s %p')
FROM dual;
-->'2017-02-09 06:00:35'
當前時間按 年 周 日 時:分:秒顯示:
SELECT DATE_FORMAT(NOW(),'%Y %b %d %T')
FROM dual;
-->'2017 Feb 09 18:04:13'
可以使用的格式有:
| 格式 | 描述 |
| %a | 縮寫星期名 |
| %b | 縮寫月名 |
| %c | 月,數(shù)值 |
| %D | 帶有英文前綴的月中的天 |
| %d | 月的天玉锌,數(shù)值(00-31) |
| %e | 月的天名挥,數(shù)值(0-31) |
| %f | 微秒 |
| %H | 小時 (00-23) |
| %h | 小時 (01-12) |
| %I | 小時 (01-12) |
| %i | 分鐘,數(shù)值(00-59) |
| %j | 年的天 (001-366) |
| %k | 小時 (0-23) |
| %l | 小時 (1-12) |
| %M | 月名 |
| %m | 月主守,數(shù)值(00-12) |
| %p | AM 或 PM |
| %r | 時間禀倔,12-小時(hh:mm:ss AM 或 PM) |
| %S | 秒(00-59) |
| %s | 秒(00-59) |
| %T | 時間, 24-小時 (hh:mm:ss) |
| %U | 周 (00-53) 星期日是一周的第一天 |
| %u | 周 (00-53) 星期一是一周的第一天 |
| %V | 周 (01-53) 星期日是一周的第一天,與 %X 使用 |
| %v | 周 (01-53) 星期一是一周的第一天参淫,與 %x 使用 |
| %W | 星期名 |
| %w | 周的天 (0=星期日, 6=星期六) |
| %X | 年救湖,其中的星期日是周的第一天,4 位涎才,與 %V 使用 |
| %x | 年鞋既,其中的星期一是周的第一天力九,4 位,與 %v 使用 |
| %Y | 年邑闺,4 位 |
| %y | 年跌前,2 位 |
4.DATEDIFF() 函數(shù)
DATEDIFF() 函數(shù)返回兩個日期之間的時間。
語法
DATEDIFF(datepart,startdate,enddate)
startdate 和 enddate 參數(shù)是合法的日期表達式陡舅。
datepart 參數(shù)可以是下列的值:
| datepart | 縮寫 |
| 年 | yy, yyyy |
| 季度 | qq, q |
| 月 | mm, m |
| 年中的日 | dy, y |
| 日 | dd, d |
| 周 | wk, ww |
| 星期 | dw, w |
| 小時 | hh |
| 分鐘 | mi, n |
| 秒 | ss, s |
| 毫秒 | ms |
| 微妙 | mcs |
| 納秒 | ns |
例子 1
使用如下 SELECT 語句:
SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate
結果:
| DiffDate |
| 1 |
例子 2
使用如下 SELECT 語句:
SELECT DATEDIFF(day,'2008-12-30','2008-12-29') AS DiffDate
結果:
| DiffDate |
| -1 |
參考:
https://www.2cto.com/database/201702/596860.html
https://blog.csdn.net/ymk0375/article/details/80059395