In MySQL, both now () and sysdate () two functions can return the current time, but there is a difference between the two. Let's take a look at the official MySQL explanation:
NOW()
Returns a constant time that indicates the time at which the statement began to execute.
This differs from the behavior SYSDATE()
for , which returns the exact time at which it executes.
The now () function returns the time at which the statement started executing, while sysdate () returns the time that the function was executed.
Here are four scenarios to understand the difference between two functions.
mysql> select now (), SLEEP (5), now (); +---- -----------------+----------+---------------------+| now () | sleep (5) | now () |+---------------------+----------+--------- ------------+| 2015-09-24 10:19:44 | 0 | 2015-09-24 10:19:44 |+---------------------+----------+---------------------+
mysql> select sysdate (), SLEEP (5), SYSDATE (); +---------------------+----------+---------------------+| sysdate () | sleep (5) | sysdate () |+---------------------+----------+---------------------+| 2015-09-24 10:20:53 | 0 | 2015-09-24 10:20:58 |+- --------------------+----------+---------------------+
mysql> select now (), SLEEP (5), SYSDATE (); + ---------------------+----------+---------------------+| now () | sleep (5) | sysdate () |+---------------------+----------+---------------------+| 2015-09-24 10:21:30 | 0 | 2015-09-24 10:21:35 |+---------------------+----------+---------------------+
Mysql> Select Sysdate (), SLEEP (5), now (), +---------------------+----------+---------------------+| Sysdate () | SLEEP (5) | Now () |+---------------------+----------+---------------------+| 2015-09-24 10:22:09 | 0 | 2015-09-24 10:22:09 |+---------------------+----------+---------------------+
The first statement: because now () returns the time that the SQL statement started executing, the result of two calls is consistent, although it sleeps for 5 seconds.
The second statement: Sysdate () returns the time that the function was called, so it sleeps for 5 seconds and two times the result of the call is 5 seconds apart.
The third statement: Execute now () returns the time that the statement starts executing, and then sleeps for 5 seconds, so two times differs by 5 seconds.
Fourth statement: Execute Sysdate () to return the time of the call, which is the time the SQL statement started executing, so two time is consistent.
MySQL now () sysdate () difference