当前位置:网站首页>MySQL日期函数
MySQL日期函数
2022-07-06 09:30:00 【My71】
下文中使用到的数据库,可参考 创建练习数据库
返回当前日期
函数:CURDATE()
指令
SELECT CURDATE();
运行结果
返回当前时间
函数:CURTIME()
指令
SELECT CURTIME();
运行结果
返回当前日期和时间
函数:NOW()
指令
SELECT NOW()
运行结果
提取日期值
函数:DATE()
指令
SELECT DATE('2021-10-31 11:36:20')
运行结果
提取年份值
函数:YEAR()
指令
SELECT YEAR('2021-10-31 11:36:20')
运行结果
提取月份值
函数:MONTH()
指令
SELECT MONTH('2021-10-31 11:36:20')
运行结果
提取天数值
函数:DAY()
指令
SELECT DAY('2021-10-31 11:36:20')
运行结果
提取小时值
函数:HOUR()
指令
SELECT HOUR('2021-10-31 11:36:20')
运行结果
提取分钟值
函数:MINUTE()
指令
SELECT MINUTE('2021-10-31 11:36:20')
运行结果
提取秒数值
函数:SECOND()
指令
SELECT SECOND('2021-10-31 11:36:20')
运行结果
将秒数转为时间
函数:SEC_TO_TIME
指令
SELECT SEC_TO_TIME(3661)
运行结果
将时间转为秒数
函数:TIME_TO_SEC()
指令
SELECT TIME_TO_SEC('1:01:01')
运行结果
计算 n 天后的日期
函数:ADDDATE(d,n)
说明:d 表示日期,n 表示天数。
指令
SELECT ADDDATE('2021-10-31',10)
运行结果
计算 n 天前的日期
函数:SUBDATE(d,n)
说明:d 表示日期,n 表示天数。
指令
SELECT SUBDATE('2021-10-31 12:23:20',10)
运行结果
计算 n 秒后的时间
函数:ADDTIME(t,n)
说明:t 表示时间,n 表示时间间隔,当 n 为整数时要小于 60,也可以用字符串表示。
指令
SELECT ADDTIME('2021-10-31 11:43:25',5),ADDTIME('2021-10-31 11:43:25','2:10:15')
运行结果
计算 n 秒前的时间
函数:SUBTIME()
说明:t 表示时间,n 表示时间间隔,当 n 为整数时要小于 60,也可以用字符串表示。
指令
SELECT SUBTIME('2021-10-31 12:23:20',10),SUBTIME('2021-10-31 12:23:20','2:3:20')
运行结果
计算日期间隔
函数:DATEDIFF(d1,d2)
说明:d1 表示结束日期,d2 表示开始日期。
指令
SELECT DATEDIFF('2022-01-01','2021-10-31'),DATEDIFF('2021-10-31','2022-01-01')
运行结果
计算时间间隔
函数:TIMEDIFF(t1,t2)
说明:t1 是开始时间 t2 结束时间
指令
SELECT TIMEDIFF('12:31:20','11:30:10')
运行结果
练习:计算 xs 表中所有人的年龄
指令
select 学号,姓名,出生日期,DATEDIFF(NOW(),出生日期) DIV 365 as 年龄 from xs;
运行结果
边栏推荐
- ~71 abbreviation attribute of font
- Some instructions on whether to call destructor when QT window closes and application stops
- 「博士毕业一年,我拿下 ACL Best Paper」
- J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
- 汇编课后作业
- When it comes to Google i/o, this is how ByteDance is applied to flutter
- QT system learning series: 1.2 style sheet sub control lookup
- Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]
- LeetCode 1561. The maximum number of coins you can get
- Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
猜你喜欢
Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance
LeetCode 1641. Count the number of Lexicographic vowel strings
我在字节跳动「修电影」
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
LeetCode 1552. Magnetic force between two balls
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games
Shell_ 03_ environment variable
字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」
字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题
随机推荐
Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
string. How to choose h and string and CString
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
~68 Icon Font introduction
redux使用说明
音视频开发面试题
视频压缩编码和音频压缩编码基本原理
Shell_ 04_ Shell script
ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
「博士毕业一年,我拿下 ACL Best Paper」
LeetCode 1984. Minimum difference in student scores
LeetCode 1551. Minimum operand to make all elements in the array equal
Hbuilder x format shortcut key settings
~84 form supplement
CMake速成
Shell_ 03_ environment variable
Solr word segmentation analysis
控制转移指令
~87 animation
Mongodb在node中的使用