轉載請注明出處 https://www.cnblogs.com/majianming/p/9647786.html
在mysql中有三個時間函數用來獲取當前的時間,分別是now()
、current_timestamp()
和 sysdate()
這三個函數都可以獲得當前的時間,例如
select now(),current_timestamp(),sysdate() \G
結果是
這里看起來是沒有什么不同的,但是翻閱mysql官方的文檔可以發現
CURRENT_TIMESTAMP
andCURRENT_TIMESTAMP()
are synonyms forNOW()
.
也就是說 CURRENT_TIMESTAMP
和 CURRENT_TIMESTAMP()
都是 NOW()
這個函數的同義詞,所以作用是一致的,而sysdate()函數的解釋是
SYSDATE()
returns the time at which it executes. This differs from the behavior forNOW()
, which returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger,NOW()
returns the time at which the function or triggering statement began to execute.)
簡單來說,now()
(current_timestamp()
)函數獲得的是語句開始執行時的時間,而sysdate()
函數是這個函數執行時候的時間。
接着舉個大家都愛舉的例子,使用sleep()
函數延時。
預期結果是延時前后now()
函數對應的時間不變,sysdate()
的時間等於之前的時間加上延時時間
select
now(),current_timestamp(),sysdate(),
sleep(5),
now(),current_timestamp(),sysdate(),
sleep(10),
now(),current_timestamp(),sysdate()
\G
結果是
與預期一致,一共耗費15秒,因為使用了兩個延時函數。
環境
MySQL 5.7.17 Community Server
參考
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html