MySQL支援的日期和時間類型有DATETIME、TIMESTAMP、DATE、TIME、YEAR ,
##幾種類型比較如下:
涉及日期和時間欄位類型選擇時,根據儲存需求選擇合適的類型即可。 2.日期和時間相關函數處理日期和時間字段的函數有很多,有的經常會在查詢中使用到,##下面介紹下幾個相關函數的使用方法:
mysql> select CURRENT_DATE,CURRENT_TIME,NOW();
+--------------+--------------+---------------------+
| CURRENT_DATE | CURRENT_TIME | NOW() |
+--------------+--------------+---------------------+
| 2020-06-03 | 15:09:37 | 2020-06-03 15:09:37 |
+--------------+--------------+---------------------+
mysql> select TO_DAYS('2020-06-03 15:09:37'),
TO_DAYS('2020-06-03')-TO_DAYS('2020-06-01');
+--------------------------------+---------------------------------------------+
| TO_DAYS('2020-06-03 15:09:37') | TO_DAYS('2020-06-03')-TO_DAYS('2020-06-01') |
+--------------------------------+---------------------------------------------+
| 737944 | 2 |
+--------------------------------+---------------------------------------------+
mysql> select MONTH('2020-06-03'),WEEK('2020-06-03'),YEAR('2020-06-03');
+---------------------+--------------------+--------------------+
| MONTH('2020-06-03') | WEEK('2020-06-03') | YEAR('2020-06-03') |
+---------------------+--------------------+--------------------+
| 6 | 22 | 2020 |
+---------------------+--------------------+--------------------+
# DATEDIFF(date1,date2) 返回起始时间 date1 和结束时间 date2 之间的天数
mysql> SELECT DATEDIFF('2017-11-30','2017-11-29') AS COL1,
-> DATEDIFF('2017-11-30','2017-12-15') AS col2;
+------+------+
| COL1 | col2 |
+------+------+
| 1 | -15 |
+------+------+
3.日期和時間欄位的規範查詢
首先,為了讓查詢更準確,在插入資料時也要依照規範來插入。為了確保年份使用4位數字且日期和月份在合理範圍內,我們建立了一個表格並插入了部分資料以方便測試。
CREATE TABLE `t_date` ( `increment_id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键', `year_col` YEAR NOT NULL COMMENT '年', `date_col` date NOT NULL COMMENT '日期', `time_col` time NOT NULL COMMENT '时间', `dt_col` datetime NOT NULL COMMENT 'datetime时间', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`increment_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='time测试表'; # 日期和时间都选取当前的日期或时间 INSERT INTO t_date (year_col,date_col,time_col,dt_col,create_time) VALUES (year(now()),DATE(NOW()),time(now()),NOW(),NOW()); # 指定日期或时间插入 INSERT INTO t_date ( `year_col`, `date_col`, `time_col`, `dt_col`, `create_time` ) VALUES ( 2020, '2020-06-03', '09:00:00', '2020-06-03 10:04:04', '2020-06-03 10:04:04' ), ( 2020, '2020-05-10', '18:00:00', '2020-05-10 16:00:00', '2020-05-10 16:00:00' ), ( 2019, '2019-10-03', '16:04:04', '2019-10-03 16:00:00', '2019-10-03 16:00:00' ), ( 2018, '2018-06-03', '16:04:04', '2018-06-03 16:00:00', '2018-06-03 16:00:00' ), ( 2000, '2000-06-03', '16:04:04', '2000-06-03 08:00:00', '2000-06-03 08:00:00' ), ( 2008, '2008-06-03', '16:04:04', '2008-06-03 08:00:00', '2008-06-03 08:00:00' ), ( 1980, '1980-06-03', '16:04:04', '1980-06-03 08:00:00', '1980-06-03 08:00:00' );
根據上面測試表的數據,我們來學習下幾種常見查詢語句的寫法:
根據日期或時間等值查詢:##
select * from t_date where year_col = 2020; select * from t_date where date_col = '2020-06-03'; select * from t_date where dt_col = '2020-06-03 16:04:04';根據日期或時間範圍查詢:
select * from t_date where date_col > '2018-01-01'; select * from t_date where dt_col >= '2020-05-01 00:00:00' and dt_col < '2020-05-31 23:59:59'; select * from t_date where dt_col between '2020-05-01 00:00:00' and '2020-05-31 23:59:59';查詢本月的資料:
# 查询create_time在本月的数据 select * from t_date where DATE_FORMAT(create_time, '%Y-%m' ) = DATE_FORMAT( CURDATE( ) , '%Y-%m' );查詢最近多少天的資料:
# 以date_col为条件 查询最近7天或30天的数据 SELECT * FROM t_date where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(date_col); SELECT * FROM t_date where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(date_col);其他各類別查詢寫法:
# 查询今天的数据 SELECT * FROM t_date WHERE TO_DAYS(create_time) = TO_DAYS(NOW()); # 查询某个月的数据 SELECT * FROM t_date WHERE DATE_FORMAT(create_time, '%Y-%m')='2020-06'; # 查询某年的数据 SELECT * FROM t_date WHERE DATE_FORMAT(create_time, '%Y')= 2020; SELECT * FROM t_date WHERE YEAR(create_time) = 2020; # 根据日期区间查询数据,并排序 SELECT * FROM t_date WHERE DATE_FORMAT(create_time, '%Y') BETWEEN '2018' AND '2020' ORDER BY create_time DESC;
以上是MySQL日期及時間字段怎麼查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!