mysql 使用相同的 now() 更新多个列
我需要更新 2 个日期时间列,并且我需要它们完全相同,使用 mysql 版本 4.1.20。我正在使用此查询:
mysql> update table set last_update=now(), last_monitor=now() where id=1;
它是安全的,还是由于对 now()
的 2 次可见调用,列可能会在不同的时间更新?
我不认为它可以用不同的值进行更新(我认为内部 mysql 每行只调用一次 now()
或类似的东西),但我不是专家,你觉得怎么样?
更新: 第二个问题是在此处提取的。
I need to update 2 datetime columns, and I need them to be exactly the same, using mysql version 4.1.20. I'm using this query:
mysql> update table set last_update=now(), last_monitor=now() where id=1;
It is safe or there is a chance that the columns are update with different time, because of the 2 visible calls to now()
?
I don't think that it can be update with different values (I think internally mysql calls now()
just once per row or something similar), but I'm not an expert, what do you think?
Update:
Second question was extracted here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
找到了解决方案:
我在 MySQL 中找到了 this文档并经过一些测试后它可以工作:
Found a solution:
I found this in MySQL Docs and after a few tests it works:
Mysql 不是很聪明。当您想在多个更新或插入查询中使用相同的时间戳时,您需要声明一个变量。
当您使用
now()
函数时,每次您在另一个查询中调用它时,系统都会调用当前时间戳。Mysql isn't very clever. When you want to use the same timestamp in multiple update or insert queries, you need to declare a variable.
When you use the
now()
function, the system will call the current timestamp every time you call it in another query.当语句开始执行时,MySQL 对每个语句计算一次 now() 。因此,每个语句有多个可见的 now() 调用是安全的。
MySQL evaluates now() once per statement when the statement commences execution. So it is safe to have multiple visible now() calls per statement.
您可以在运行更新查询之前将 now() 的值存储在变量中,然后使用该变量来更新字段
last_update
和last_monitor
。这将确保 now() 仅执行一次,并且在您需要的两列上更新相同的值。
You can store the value of a now() in a variable before running the update query and then use that variable to update both the fields
last_update
andlast_monitor
.This will ensure the now() is executed only once and same value is updated on both columns you need.
您可以将以下代码放在时间戳列的默认值上:
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
,因此更新时两列采用相同的值。You can put the following code on the default value of the timestamp column:
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
, so on update the two columns take the same value.如果您确实需要确保 now() 具有相同的值,您可以运行两个查询(这也将回答您的第二个问题,在这种情况下,您要求更新 last_monitor =到last_update,但
last_update
尚未更新)你可以这样做:
无论如何,我认为mysql足够聪明,只需要
now()
每个查询一次。If you really need to be sure that
now()
has the same value you can run two queries (that will answer to your second question too, in that case you are asking toupdate last_monitor = to last_update
butlast_update
hasn't been updated yet)you could do something like:
anyway I think that mysql is clever enough to ask for
now()
only once per query.有两种方法:
首先,我建议您在将 now() 注入到 sql 语句之前将其声明为变量。可以说;
从逻辑上讲,如果您想要last_monitor有不同的输入,那么您将添加另一个变量,例如;
这样,您可以多次使用变量,不仅在 mysql 语句中,而且在您项目中使用的服务器端脚本语言(如 PHP)中。
请记住,这些相同的变量可以作为输入插入到应用程序前端的表单中。这使得项目是动态的而不是静态的。
其次如果 now() 指示更新时间,那么使用 mysql 您可以将行的属性标记为时间戳。每次插入或更新行时,时间也会更新。
There are 2 ways to this;
First, I would advice you declare now() as a variable before injecting it into the sql statement. Lets say;
Logically if you want a different input for last_monitor then you will add another variable like;
This way you can use the variables as many times as you can, not only in mysql statements but also in the server-side scripting-language(like PHP) you are using in your project.
Remember these same variables can be inserted as inputs in a form on the front-end of the application. That makes the project dynamic and not static.
Secondly if now() indicates time of update then using mysql you can decalre the property of the row as a timestamp. Every time a row is inserted or updated time is updated too.