mysql 中 時間函數 now() current_timestamp() 和 sysdate() 比較


轉載請注明出處 https://www.cnblogs.com/majianming/p/9647786.html

在mysql中有三個時間函數用來獲取當前的時間,分別是now()current_timestamp()sysdate()

這三個函數都可以獲得當前的時間,例如

select now(),current_timestamp(),sysdate()  \G

結果是

這里看起來是沒有什么不同的,但是翻閱mysql官方的文檔可以發現

CURRENT_TIMESTAMP and CURRENT_TIMESTAMP() are synonyms for NOW().

也就是說 CURRENT_TIMESTAMPCURRENT_TIMESTAMP()都是 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. (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

轉載請注明出處 https://www.cnblogs.com/majianming/p/9647786.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM