当前位置:网站首页>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;运行结果

边栏推荐
- Codeforces Global Round 19
- JS encapsulates the method of array inversion -- Feng Hao's blog
- 登陆验证koa-passport中间件的简单使用
- Error: case label `15 'not within a switch statement
- 我走過最迷的路,是字節跳動程序員的腦回路
- Shell_ 02_ Text three swordsman
- Hbuilder x format shortcut key settings
- The 116 students spent three days reproducing the ByteDance internal real technology project
- 亮相Google I/O,字节跳动是这样应用Flutter的
- Data config problem: the reference to entity 'useunicode' must end with ';' delimiter.
猜你喜欢

7-5 blessing arrived

Ffmpeg command line use

Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.

~87 animation

Error occurred during initialization of VM Could not reserve enough space for object heap

Some instructions on whether to call destructor when QT window closes and application stops

亮相Google I/O,字节跳动是这样应用Flutter的

~68 Icon Font introduction

搭建flutter环境入坑集合

Cmake Express
随机推荐
DS18B20数字温度计系统设计
Continue and break jump out of multiple loops
SQL quick start
How to generate six digit verification code
After the subscript is used to assign a value to the string type, the cout output variable is empty.
LeetCode 1638. Count the number of substrings with only one character difference
Detailed explanation of FLV format
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
LeetCode 1552. Magnetic force between two balls
Shell_ 02_ Text three swordsman
J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
7-6 sum of combinatorial numbers
Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021
Solr standalone installation
7-5 blessing arrived
Introduction to microservices
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
JS time function Daquan detailed explanation ----- AHAO blog
Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
~74 JD top navigation bar exercise