0xD*_*EEF 3 sql time timestamp
说我有长时间运行更新查询
update some_table
set modification_time = now()
where (something incredibly complex);
Run Code Online (Sandbox Code Playgroud)
some_table中的modification_time值是多少?它们是相同还是不同(例如,执行查询需要2天).
如果它们不同,我该如何编写这个查询以使它们都相同?
它们都是相同的,因为NOW()在查询开始时被锁定.
这个答案太短了吗?
好的,更多信息NOW()的MySQL参考
NOW()返回一个常量时间,指示语句开始执行的时间.(在存储的函数或触发器中,NOW()返回函数或触发语句开始执行的时间.)这与SYSDATE()的行为不同,后者返回执行它的确切时间.
实际上,阅读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 |
+---------------------+----------+---------------------+
Run Code Online (Sandbox Code Playgroud)
你问的有什么有趣的.. 注意你可以在查询中休眠? 考虑这个查询(子查询只是模拟一个3记录表)
select *, now(), sleep(2), sysdate()
from (select 1 N union all select 2 union all select 3) M
Run Code Online (Sandbox Code Playgroud)
你得到:
N now() sleep(2) sysdate()
1 2011-04-02 23:55:27 0 2011-04-02 23:55:29
2 2011-04-02 23:55:27 0 2011-04-02 23:55:31
3 2011-04-02 23:55:27 0 2011-04-02 23:55:33
Run Code Online (Sandbox Code Playgroud)