MySQL에서 지원하는 날짜 및 시간 유형은 DATETIME, TIMESTAMP, DATE, TIME, YEAR,
여러 유형을 비교하면 다음과 같습니다.
날짜 포함 및 시간 필드 유형을 선택할 때 스토리지 요구 사항에 따라 적절한 유형을 선택하면 됩니다.
날짜 및 시간 필드를 처리하는 함수가 많이 있으며 그 중 일부는 쿼리에 자주 사용됩니다.
다음은 여러 관련 함수를 사용하는 방법을 소개합니다.
CURDATE와 CURRENT_DATE 두 함수는 동일한 효과를 가지며 현재 시스템의 날짜 값을 반환합니다.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 | +------+------+
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 중국어 웹사이트의 기타 관련 기사를 참조하세요!