MySQL 中 NOW()、SYSDATE() 和 CURRENT_DATE() 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24137752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:38:11  来源:igfitidea点击:

Difference between NOW(), SYSDATE() & CURRENT_DATE() in MySQL

mysqldatabasedatetime

提问by Ashutosh SIngh

What difference between NOW(), SYSDATE(), CURRENT_DATE()in MySQL and where it can be used in real scenario .

NOW(), SYSDATE(),CURRENT_DATE()在 MySQL 中和在实际场景中可以使用的地方有什么区别。

I tried NOW(),SYSDATE(),Current_Date()when I insert data into a table and column datatype is TIMESTAMPall are given same date and time.

我想NOW()SYSDATE()Current_Date()当我将数据插入到一个表和字段的数据类型是TIMESTAMP所有被赋予相同的日期和时间。

回答by Benoit

Current_date() will only give you the date.
now() give you the datetime when the statement,procedure etc... started.
sysdate() give you the current datetime.
Look at the seconds after waiting 5 seconds between now()1 sysdate()1 with the following query (scroll to the right):

Current_date() 只会给你日期。
now() 为您提供语句、程序等...开始的日期时间。
sysdate() 为您提供当前日期时间。
使用以下查询(向右滚动)查看 now()1 sysdate()1 之间等待 5 秒后的秒数:

select now(),sysdate(),current_date(),sleep(5),now(),sysdate();

-- will give
-- now()    sysdate()   current_date()  sleep(5)    now()1  sysdate()1
-- 6/10/2014 2:50:04 AM 6/10/2014 2:50:04 AM    6/10/2014 12:00:00 AM   0   6/10/2014 2:50:04 AM 6/10/2014 2:50:09 AM

回答by Avijit batabyal

NOW()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.) This differs from the behavior for SYSDATE(), which returns the exact time at which it executes.

NOW()返回一个常量时间,指示语句开始执行的时间。(在存储的函数或触发器中,NOW()返回函数或触发语句开始执行的时间。)这与 的行为不同SYSDATE(),后者返回它执行的确切时间。

mysql> SELECT NOW(), SLEEP(2), NOW();
+---------------------+----------+---------------------+
| NOW()               | SLEEP(2) | NOW()               |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 |        0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+

mysql> SELECT SYSDATE(), SLEEP(2), SYSDATE();
+---------------------+----------+---------------------+
| SYSDATE()           | SLEEP(2) | SYSDATE()           |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 |        0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+

回答by Dax

Current_date returns the time stamp of the client while sysdate returns the time stamp of the server. If both server and the client are on the same machine, then, the result of both commands are the same. But in case that your sever is for example in USA and your clients are in China, then, these two functions return completely different results.

Current_date 返回客户端的时间戳,而 sysdate 返回服务器的时间戳。如果服务器和客户端都在同一台机器上,那么两个命令的结果是一样的。但是,如果您的服务器在美国,而您的客户在china,那么这两个函数将返回完全不同的结果。

I don't know about thew now(), sorry :-)

我现在不知道 (),对不起 :-)

回答by Islay

CURRENT_DATE()is a synonym for many other similar functions all of which provide only the date. There is a subtle difference between NOW()and SYSDATE()which you can read up more on this
official MySQL websitepage.

CURRENT_DATE()是许多其他类似功能的同义词,所有这些功能都只提供日期NOW()和之间存在细微差别SYSDATE(),您可以在此
MySQL 官方网站页面上阅读更多内容。

回答by Divija

NOW() returns a constant time that indicate's the time at which the statement began to exicute whereas SYSDATE () returns the time at which it exicute... OR in other words NOW ()shows query exicution time and SYSDATE() shows self exicution time..

NOW() 返回一个常数时间,指示语句开始执行的时间,而 SYSDATE () 返回它执行的时间......或者换句话说 NOW () 显示查询执行时间而 SYSDATE() 显示自执行时间..