当前位置:网站首页>MySQL中的时间函数
MySQL中的时间函数
2022-07-29 08:11:00 【Bejpse】
目录
1. 获取系统当前时间
MySQL 版本为 5.7,详细的时间函数可以参考 MySQL 官方文档 在这里
1.1. 获取 YYYY-MM-DD HH:mm:ss
SELECT NOW(),CURRENT_TIMESTAMP(),SYSDATE(),CURRENT_TIMESTAMP;

NOW()返回当前日期和时间CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP都是NOW()函数的同义词NOW()在执行开始时值就得到了SYSDATE()返回函数执行的时间,一般情况下很少用到
1.2. 获取 YYYY-MM-DD
SELECT CURRENT_DATE,CURRENT_DATE(),CURDATE(),DATE(NOW());

CURDATE()返回当前日期CURRENT_DATE(),CURRENT_DATE都是CURDATE()的同义词DATE()提取日期或日期时间表达式的日期部分
1.3. 获取 HH:mm:ss
SELECT CURRENT_TIME,CURRENT_TIME(),CURTIME(),TIME(NOW());

CURTIME()返回当前时间CURRENT_TIME(),CURRENT_TIME都是CURTIME()的同义词TIME()提取传递的表达式的时间部分
2. 时间加减间隔函数
MySQL 中内置函数 DATE_ADD() 和 DATE_SUB() 能对指定的时间进行增加或减少一个指定的时间间隔,语法如下
DATE_ADD(date,INTERVAL expr type)
DATE_SUB(date,INTERVAL expr type)
date是指定的日期INTERVAL为关键词expr是具体的时间间隔type是时间单位
注意:type 可以是复合型的,比如 YEAR_MONTH。如果 type 不是复合型的, DATE_ADD() 和 DATE_SUB() 其实可以通用,因为 expr 可以为一个负数。可用的 type 如下表
MICROSECOND
间隔单位:毫秒
SECOND
间隔单位:秒
MINUTE
间隔单位:分钟
HOUR
间隔单位:小时
DAY
间隔单位:天
WEEK
间隔单位:星期
MONTH
间隔单位:月
QUARTER
间隔单位:季度
YEAR
间隔单位:年
SECOND_MICROSECOND
复合型,间隔单位:秒、毫秒,expr可以用两个值来分别指定秒和毫秒
MINUTE_MICROSECOND
复合型,间隔单位:分、毫秒
MINUTE_SECOND
复合型,间隔单位:分、秒
HOUR_MICROSECOND
复合型,间隔单位:小时、毫秒
HOUR_SECOND
复合型,间隔单位:小时、秒
HOUR_MINUTE
复合型,间隔单位:小时分
DAY_MICROSECOND
复合型,间隔单位:天、毫秒
DAY_SECOND
复合型,间隔单位:天、秒
DAY_MINUTE
复合型,间隔单位:天、分
DAY_HOUR
复合型,间隔单位:天、小时
YEAR_MONTH
复合型,间隔单位:年、月
2.1. DATETIME 类型的加减
-- 给当前的时间日期增加一个月
SELECT DATE_ADD(NOW(),INTERVAL 1 MONTH), NOW();

-- 给当前的时间日期减少一个月
SELECT DATE_SUB(NOW(),INTERVAL 1 MONTH), NOW();

2.2. DATE 类型的加减
-- 给当前的日期增加 10 天
SELECT DATE_ADD(DATE(NOW()),INTERVAL 10 DAY), DATE(NOW());

-- 给当前的日期减少 10 天
SELECT DATE_SUB(DATE(NOW()),INTERVAL 10 DAY), DATE(NOW());

3. 两个时间的相减
3.1. DATE 类型相减
DATEDIFF(date1, date2) 减去两个日期,比较的是天数,与时间无关 date1 - date2
SELECT DATEDIFF('2013-01-13','2012-10-01');

SELECT DATEDIFF('2013-01-13 13:13:13','2012-10-01 16:16:16');

SELECT DATEDIFF('13:13:13','16:16:16');

3.2. TIMESTAMP 类型
TIMESTAMPDIFF(type, ts1, ts2) : 根据 type,计算两个时间 ts2 - ts1 相差多少天、月、年等
SELECT TIMESTAMPDIFF(DAY, '2013-01-13','2012-10-01');

SELECT TIMESTAMPDIFF(MONTH, '2013-01-13 13:13:13','2012-10-01 16:16:16');

SELECT TIMESTAMPDIFF(HOUR, '13:13:13','16:16:16');

MySQL 关于时间函数的官方文档:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
边栏推荐
- Crawl notes
- Stm32ff030 replaces domestic MCU dp32g030
- CentOS deploy PostgreSQL 13
- Redshift 2.6.41 for maya2018 watermark removal
- Crawl expression bag
- [cryoelectron microscope] relation4.0 - subtomogram tutorial
- Compare three clock circuit schemes of single chip microcomputer
- [noi simulation] computational geometry (convex hull, violence, and search set)
- (视频+图文)机器学习入门系列-第5章 机器学习实践
- MySQL rownum implementation
猜你喜欢

Simplefoc+platformio stepping on the path of the pit

Useful websites

Arduino uno error analysis avrdude: stk500_ recv(): programmer is not responding

Day 014 2D array exercise

Character shader exercise

Detailed explanation of two modes of FTP

A problem encountered in SQL interview

sql判断语句的编写

Dynamic thresholds buffer management in a shared buffer packet switch paper summary

UE4 highlight official reference value
随机推荐
Unity beginner 4 - frame animation and protagonist attack (2D)
Network Security Learning chapter
STM32 printf problem summary semihosting microlib understanding
[cryoelectron microscope | paper reading] emclarity: software for high-resolution cryoelectron tomography and sub fault averaging
[cryptography experiment] 0x00 install NTL Library
Shell script - global variables, local variables, environment variables
What is Amazon self support number and what should sellers do?
Tcp/ip five layer reference model and corresponding typical devices and IPv6
Detailed explanation of the find command (the most common operation of operation and maintenance at the end of the article)
(视频+图文)机器学习入门系列-第5章 机器学习实践
Limitations of push down analysis
Unity beginner 1 - character movement control (2D)
Solve the problem that the disk is full due to large files
torch.nn.functional.one_ hot()
Stm8s003 domestic substitute for dp32g003 32-bit microcontroller chip
亚马逊测评自养号是什么,卖家应该怎么做?
[dry goods memo] 50 kinds of Matplotlib scientific research paper drawing collection, including code implementation
Implementation of simple cubecap+fresnel shader in unity
An optimal buffer management scheme with dynamic thresholds paper summary
Cyberpunk special effect shader