mysql计算时间差函数

发布时间: 2023-11-21 10:58 阅读: 文章来源:1MUMB604PS

前面,两篇分别介绍了,MySQL的字符串函数和数值函数,接下来,一起学习下日期函数,走起[奸笑]

上图

1.NOW()获取当前时间

SQL语句:select NOW() as temporary_time

2.YEAR()获取当前年份

SQL语句:select YEAR(NOW()) as temporary_time

3.MONTH()获取当前月份

SQL语句:select MONTH(NOW()) as temporary_time

4.WEEK()获取当前是今年的第几周

SQL语句:select WEEK(NOW()) as temporary_time

5.DAY()获取本月几号

SQL语句:select DAY(NOW()) as temporary_time

6.HOUR()获取当前小时

SQL语句:select HOUR(NOW()) as temporary_time

7.MINUTE()获取当前分钟

SQL语句:select MINUTE(NOW()) as temporary_time

8.MONTHNAME()返回英文月份

SQL语句:select MONTHNAME(NOW()) as temporary_time

9.CURDATE()返回当前日期,只包含年月日

SQL语句:select CURDATE() as temporary_time

10.CURTIME() 返回当前时间,只包含时分秒

SQL语句:select CURTIME() as temporary_time

11.UNIX_TIMESTAMP()返回时间戳

SQL语句:select UNIX_TIMESTAMP(NOW()) as temporary_time

12.FROM_UNIXTIME()返回对应时间戳的日期值

SQL语句:select FROM_UNIXTIME(1612166213) as temporary_time

13.DATE_FORMAT(date,format)格式化 日期函数

SQL语句:select DATE_FORMAT(NOW(),‘%Y-%m-%d‘) as temporary_time

14. DATEDIFF(x,y) 计算日期相差多少天,y-x

SQL语句:select DATEDIFF(‘1995-11-06‘,‘1994-09-22‘) as temporary_time

收工[鼓掌]

年前最后一更[奸笑]

新年快乐!

祝各位:新的一年,恭喜发财,万事如意[撒花]

来年我们一起努力!

•••展开全文