mysql日期时间函数总结(附带mysql官网介绍)

MySQL提供了大量丰富的函数,在MySQL的使用中经常会用到各种函数,本文记录的是常见的MySQL的日期与时间函数,主要用于处理日期和时间值。

MySQL 获得当前日期+时间 函数

获得当前日期+时间(date + time)函数:now(),SYSDATE();

SELECT now(), SYSDATE();

sysdate() 日期时间函数跟 now() 类似,区别在于:now() 在执行开始时值就得到了,得到的是一个不变的、恒定的时间, sysdate() 在函数执行时动态得到值。原文是这样的(SYSDATE() returns the time at which it executes. This differs from the behavior for NOW(), which returns a constant time that indicates the time at which the statement began to execute.)可查看官网的例子,如下:

mysql> SELECT NOW(), SLEEP(2), NOW();
+---------------------+----------+---------------------+
| NOW()               | SLEEP(2) | NOW()               |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 |        0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+

mysql> SELECT SYSDATE(), SLEEP(2), SYSDATE();
+---------------------+----------+---------------------+
| SYSDATE()           | SLEEP(2) | SYSDATE()           |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 |        0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+

可以看到,虽然中途 sleep 2 秒,但 now() 函数两次的时间值是相同的; sysdate() 函数两次得到的时间值相差 2 秒。MySQL Manual 中是这样描述 sysdate() 的:Return the time at which the function executes。
sysdate() 日期时间函数,一般情况下很少用到。 

MySQL 获得当前时间戳 函数

MySQL 获得当前时间戳 函数:current_timestamp, current_timestamp()

SELECT current_timestamp, current_timestamp();


MySQL 日期转换函数、时间转换函数

MySQL Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format)(日期转换), time_format(time,format)(时间转换)

SELECT DATE_FORMAT(now(), '%Y-%m-%d %H:%i:%s'), TIME_FORMAT(now(),'%Y-%m-%d %H:%i:%s');

从结果可知,time_format(time,format)函数:只转换时间,不转换日期

MySQL 日期、时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。

MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)

select str_to_date('08/09/2019', '%m/%d/%Y'); -- 2019-08-09
select str_to_date('08/09/19' , '%m/%d/%y'); -- 2019-08-09
select str_to_date('08.09.2019', '%m.%d.%Y'); -- 2019-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2019 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2019-08-09 08:09:30

MySQL 日期、时间相减函数

DATEDIFF(date1,date2), TIMEDIFF(time1,time2)

MySQL DATEDIFF(date1,date2):两个日期相减 date1 - date2,返回天数。
SELECT DATEDIFF('2020-03-08', '2020-03-01'); -- 7
SELECT DATEDIFF('2020-03-01', '2020-03-08'); -- -7

MySQL TIMEDIFF(time1,time2):两个日期相减 time1 - time2,返回 time 差值。
SELECT TIMEDIFF('2020-03-25 17:51:25', '2020-03-25 17:50:20'); -- 00:01:05
SELECT TIMEDIFF('2020-03-25 17:50:20', '2020-03-25 17:51:25'); -- 00:01:05

注意:timediff(time1,time2) 函数的两个参数类型必须相同。

【参考资料】

1、Mysql官网介绍--12.6 Date and Time Functions(日期和时间函数): https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_get-format

2、MySQL:日期函数、时间函数总结(MySQL 5.X):https://www.cnblogs.com/she27/archive/2009/01/16/1377089.html

3、Mysql基础,常用日期时间函数详解:https://baijiahao.baidu.com/s?id=1621234901544582551&wfr=spider&for=pc

发布了146 篇原创文章 · 获赞 221 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_44299027/article/details/104651266