记录一下使用Mysql获取当前日期时间的方式
获取当前完整的日期时间有常见的四种方式,获取得到的默认格式(mysql的格式标准)是
%Y-%m-%d %H:%i:%s
其它格式
%Y-%m-%d %H:%i:%s.%f
方式一:now()函数
select now();
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2025-03-30 11:07:51 |
+---------------------+
1 row in set (0.00 sec)
方式二:sysdate()函数
select sysdate();
mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| 2025-03-30 11:07:53 |
+---------------------+
1 row in set (0.00 sec)
方式三:current_timestamp系统变量
select current_timestamp;
mysql> select current_timestamp;
+---------------------+
| current_timestamp |
+---------------------+
| 2025-03-30 11:07:55 |
+---------------------+
1 row in set (0.00 sec)
方式四:current_timestamp()函数
select current_timestamp();
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2025-03-30 11:07:59 |
+---------------------+
1 row in set (0.00 sec)
mysql>
now()、sysdate()、current_timestamp和 current_timestamp() 四种方式都可以用来获取当前时间
平常使用看不出任何的区别,因为执行时间极短的
now()的特点是在一个语句中,不管出现几个now(),在整个执行过程中是固定的,比如一个查询中包含多个 now() 调用,它们的值都相同, 所以可以用在一致性日期时间的场景里
mysql高版本支持now()函数查询微秒精度,那么可以很明显验证now()在语句中的一致性日期时间
SELECT NOW(6), NOW(6);
mysql> SELECT NOW(6), NOW(6);
+----------------------------+----------------------------+
| NOW(6) | NOW(6) |
+----------------------------+----------------------------+
| 2025-03-30 11:39:45.543973 | 2025-03-30 11:39:45.543973 |
+----------------------------+----------------------------+
1 row in set (0.00 sec)
sysdate()特点是它的值是动态的,在一个语句中,每次调用 sysdate() 都会返回当前系统的时间,目测是看不出来的,增加上它的微秒精度,效果直接可以看到,微秒级别的数据是不一致的
SELECT SYSDATE(6), SYSDATE(6);
mysql> SELECT SYSDATE(6), SYSDATE(6);
+----------------------------+----------------------------+
| SYSDATE(6) | SYSDATE(6) |
+----------------------------+----------------------------+
| 2025-03-30 11:46:36.856344 | 2025-03-30 11:46:36.856368 |
+----------------------------+----------------------------+
1 row in set (0.00 sec)
mysql>
current_timestamp和 current_timestamp(),一个是执行变量关键字,一个是函数,它们和now()差不多,返回值在整个语句中是固定的,至于到底有什么区别,又与其它函数有啥细微区别不再做计较,作为使用着,不研究那么深。
select current_timestamp(6), current_timestamp(6);
mysql> select current_timestamp(6), current_timestamp(6);
+----------------------------+----------------------------+
| current_timestamp(6) | current_timestamp(6) |
+----------------------------+----------------------------+
| 2025-03-30 11:56:09.357913 | 2025-03-30 11:56:09.357913 |
+----------------------------+----------------------------+
1 row in set (0.00 sec)
mysql>
now(n), sysdate(n), current_timestamp(n) 这个n可以是1-6
mysql> SELECT NOW(1), NOW(1);
+-----------------------+-----------------------+
| NOW(1) | NOW(1) |
+-----------------------+-----------------------+
| 2025-03-30 12:19:37.0 | 2025-03-30 12:19:37.0 |
+-----------------------+-----------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW(2), NOW(2);
+------------------------+------------------------+
| NOW(2) | NOW(2) |
+------------------------+------------------------+
| 2025-03-30 12:19:41.65 | 2025-03-30 12:19:41.65 |
+------------------------+------------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW(3), NOW(3);
+-------------------------+-------------------------+
| NOW(3) | NOW(3) |
+-------------------------+-------------------------+
| 2025-03-30 12:19:46.949 | 2025-03-30 12:19:46.949 |
+-------------------------+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW(4), NOW(4);
+--------------------------+--------------------------+
| NOW(4) | NOW(4) |
+--------------------------+--------------------------+
| 2025-03-30 12:19:52.4370 | 2025-03-30 12:19:52.4370 |
+--------------------------+--------------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW(5), NOW(5);
+---------------------------+---------------------------+
| NOW(5) | NOW(5) |
+---------------------------+---------------------------+
| 2025-03-30 12:19:57.98093 | 2025-03-30 12:19:57.98093 |
+---------------------------+---------------------------+
1 row in set (0.00 sec)
mysql> SELECT SYSDATE(6), SYSDATE(6);
+----------------------------+----------------------------+
| SYSDATE(6) | SYSDATE(6) |
+----------------------------+----------------------------+
| 2025-03-30 12:20:01.351322 | 2025-03-30 12:20:01.351346 |
+----------------------------+----------------------------+
1 row in set (0.01 sec)
mysql>