当前位置:网站首页>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;
运行结果
边栏推荐
- Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
- Continue and break jump out of multiple loops
- 这116名学生,用3天时间复刻了字节跳动内部真实技术项目
- Full record of ByteDance technology newcomer training: a guide to the new growth of school recruitment
- ~74 JD top navigation bar exercise
- Introduction to microservices
- LeetCode 1551. Minimum operand to make all elements in the array equal
- README. txt
- [unsolved] 7-15 shout mountain
- 汇编语言寻址方式
猜你喜欢
Solr word segmentation analysis
I'm "fixing movies" in ByteDance
Audio and video development interview questions
~Introduction to form 80
~87 animation
was unable to send heartbeat
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
~84 form supplement
Some instructions on whether to call destructor when QT window closes and application stops
~79 Movie card exercise
随机推荐
Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?
~75 background
ByteDance technical Interviewer: what kind of candidate do I want to pick most
SQL快速入门
JS time function Daquan detailed explanation ----- AHAO blog
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
Error: case label `15 'not within a switch statement
SQL quick start
Solr new core
string. How to choose h and string and CString
The 116 students spent three days reproducing the ByteDance internal real technology project
汇编语言段定义
算数运算指令
7-5 blessing arrived
DS18B20數字溫度計系統設計
「博士毕业一年,我拿下 ACL Best Paper」
~68 Icon Font introduction
~83 form introduction
Restful style interface design
Two weeks' experience of intermediate software designer in the crash soft exam