記錄一下使用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>