MySQL now () sysdate () difference

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.