mysql 使用相同的 now() 更新多个列

发布于 2024-09-25 05:07:57 字数 467 浏览 3 评论 0原文

我需要更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

吹梦到西洲 2024-10-02 05:07:58

找到了解决方案:

mysql> UPDATE table SET last_update=now(), last_monitor=last_update WHERE id=1;

我在 MySQL 中找到了 this文档并经过一些测试后它可以工作:

以下语句将 col2 设置为当前(更新的)col1 值,而不是原始 col1 值。结果是 col1 和 col2
具有相同的值。此行为与标准 SQL 不同。

更新 t1 SET col1 = col1 + 1,col2 = col1;

Found a solution:

mysql> UPDATE table SET last_update=now(), last_monitor=last_update WHERE id=1;

I found this in MySQL Docs and after a few tests it works:

the following statement sets col2 to the current (updated) col1 value, not the original col1 value. The result is that col1 and col2
have the same value. This behavior differs from standard SQL.

UPDATE t1 SET col1 = col1 + 1, col2 = col1;

妖妓 2024-10-02 05:07:58

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.

墟烟 2024-10-02 05:07:58

当语句开始执行时,MySQL 对每个语句计算一次 now() 。因此,每个语句有多个可见的 now() 调用是安全的。

select now(); select now(), sleep(10), now(); select now();
+---------------------+
| now()               |
+---------------------+
| 2018-11-05 16:54:00 |
+---------------------+
1 row in set (0.00 sec)

+---------------------+-----------+---------------------+
| now()               | sleep(10) | now()               |
+---------------------+-----------+---------------------+
| 2018-11-05 16:54:00 |         0 | 2018-11-05 16:54:00 |
+---------------------+-----------+---------------------+
1 row in set (10.00 sec)

+---------------------+
| now()               |
+---------------------+
| 2018-11-05 16:54:10 |
+---------------------+
1 row in set (0.00 sec)

MySQL evaluates now() once per statement when the statement commences execution. So it is safe to have multiple visible now() calls per statement.

select now(); select now(), sleep(10), now(); select now();
+---------------------+
| now()               |
+---------------------+
| 2018-11-05 16:54:00 |
+---------------------+
1 row in set (0.00 sec)

+---------------------+-----------+---------------------+
| now()               | sleep(10) | now()               |
+---------------------+-----------+---------------------+
| 2018-11-05 16:54:00 |         0 | 2018-11-05 16:54:00 |
+---------------------+-----------+---------------------+
1 row in set (10.00 sec)

+---------------------+
| now()               |
+---------------------+
| 2018-11-05 16:54:10 |
+---------------------+
1 row in set (0.00 sec)
属性 2024-10-02 05:07:58

您可以在运行更新查询之前将 now() 的值存储在变量中,然后使用该变量来更新字段 last_updatelast_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 and last_monitor.

This will ensure the now() is executed only once and same value is updated on both columns you need.

清醇 2024-10-02 05:07:58

您可以将以下代码放在时间戳列的默认值上:
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.

滥情空心 2024-10-02 05:07:58

如果您确实需要确保 now() 具有相同的值,您可以运行两个查询(这也将回答您的第二个问题,在这种情况下,您要求更新 last_monitor =到last_update,但last_update尚未更新)

你可以这样做:

mysql> update table set last_update=now() where id=1;
mysql> update table set last_monitor = last_update where id=1;

无论如何,我认为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 to update last_monitor = to last_update but last_update hasn't been updated yet)

you could do something like:

mysql> update table set last_update=now() where id=1;
mysql> update table set last_monitor = last_update where id=1;

anyway I think that mysql is clever enough to ask for now() only once per query.

仙气飘飘 2024-10-02 05:07:58

有两种方法:

首先,我建议您在将 now() 注入到 sql 语句之前将其声明为变量。可以说;

var x = now();
mysql> UPDATE table SET last_update=$x, last_monitor=$x WHERE id=1;

从逻辑上讲,如果您想要last_monitor有不同的输入,那么您将添加另一个变量,例如;

var y = time();
mysql> UPDATE table SET last_update=$x, last_monitor=$y WHERE id=1;

这样,您可以多次使用变量,不仅在 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;

var x = now();
mysql> UPDATE table SET last_update=$x, last_monitor=$x WHERE id=1;

Logically if you want a different input for last_monitor then you will add another variable like;

var y = time();
mysql> UPDATE table SET last_update=$x, last_monitor=$y WHERE id=1;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文