MySQL 当运行 UPDATE ... datetime = NOW(); 所有更新的行都具有相同的日期/时间吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/195173/
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 12:12:45  来源:igfitidea点击:

When running UPDATE ... datetime = NOW(); will all rows updated have the same date/time?

mysqlsqldatetimesql-update

提问by Darryl Hein

When you run something similar to:

当您运行类似以下内容时:

UPDATE table SET datetime = NOW();

on a table with 1 000 000 000 records and the query takes 10 seconds to run, will all the rows have the exact same time (minutes and seconds) or will they have different times? In other words, will the time be when the query started or when each row is updated?

在具有 1 000 000 000 条记录且查询需要 10 秒运行的表上,所有行的时间是否完全相同(分钟和秒)还是它们的时间不同?换句话说,时间是查询开始的时间还是每行更新的时间?

I'm running MySQL, but I'm thinking this applies to all dbs.

我正在运行 MySQL,但我认为这适用于所有 dbs。

回答by micahwittman

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now

"NOW() returns a constant time that indicates the time at which the statement began to execute. (Within a stored routine or trigger, NOW() returns the time at which the routine or triggering statement began to execute.) This differs from the behavior for SYSDATE(), which returns the exact time at which it executes as of MySQL 5.0.13. "

“NOW() 返回一个常数时间,指示语句开始执行的时间。(在存储例程或触发器中,NOW() 返回例程或触发语句开始执行的时间。)这与SYSDATE() 的行为,它返回它从 MySQL 5.0.13 开始执行的确切时间。”

回答by MRRaja

Assign NOW()to a variable then update the datetime with variable:

分配NOW()给一个变量,然后用变量更新日期时间:

update_date_time=now()

now update like this

现在像这样更新

UPDATE table SET datetime =update_date_time;

correct the syntax, as per your requirement

根据您的要求更正语法

回答by andora

If the end-result is important to you - TEST IT FIRST, just because it 'ought' to work as documented, doesn't mean it will. If in doubt, test!

如果最终结果对您很重要 - 首先测试它,仅仅因为它“应该”按照文档工作,并不意味着它会。如有疑问,请测试!

回答by gizmo

They should have the same time, the update is supposed to be atomic, meaning that whatever how long it takes to perform, the action is supposed to occurs as if all was done at the same time.

他们应该有相同的时间,更新应该是原子的,这意味着无论执行需要多长时间,动作都应该发生,就好像所有事情都是在同一时间完成的。

If you're experiencing a different behaviour, it's time to change for another DBMS.

如果您遇到不同的行为,则是时候为另一个 DBMS 进行更改了。

回答by My Name Goes Here

The sqlite answer is

sqlite的答案是

update TABLE set mydatetime = datetime('now');

in case someone else was looking for it.

以防其他人正在寻找它。