欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【MySQL】日期、时间相关

程序员文章站 2024-01-28 19:59:52
...

参考:MySQL:日期函数、时间函数总结

now()

获得当前日期+时间(date + time)

select now();
+---------------------+
| now()               |
+---------------------+
| 2019-10-29 17:02:59 |
+---------------------+
1 row in set (0.00 sec)

sysdate()

获得当前日期+时间(date + time),与now()不同之处如下:

mysql> select sysdate(), sleep(3), sysdate();
+---------------------+----------+---------------------+
| sysdate()           | sleep(3) | sysdate()           |
+---------------------+----------+---------------------+
| 2019-10-29 17:05:19 |        0 | 2019-10-29 17:05:22 |
+---------------------+----------+---------------------+
1 row in set (3.07 sec)
mysql> select now(), sleep(3), now();
+---------------------+----------+---------------------+
| now()               | sleep(3) | now()               |
+---------------------+----------+---------------------+
| 2019-10-29 17:05:37 |        0 | 2019-10-29 17:05:37 |
+---------------------+----------+---------------------+
1 row in set (3.06 sec)

TO_DAYS(date)

给定日期,返回对应天数(年为0以来的天数)。

mysql> select TO_DAYS(20101029);
+-------------------+
| TO_DAYS(20101029) |
+-------------------+
|            734439 |
+-------------------+
1 row in set (0.01 sec)