NOW()是否可以保证DATETIME InnoDB事务? [英] NOW() for DATETIME InnoDB Transaction guaranteed?

查看:43
本文介绍了NOW()是否可以保证DATETIME InnoDB事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否在单个InnoDB事务中的2个以上查询中使用NOW()保证,以确保插入的datetime值在数据库中是准确的?

Does using NOW() in 2+ queries in a single InnoDB transaction guarantee that the inserted datetime value will be exact in the database?

换句话说,即使您在单个事务中使用该事务的查询超过20个,NOW()也会始终相同,还是会改变?

In other words, is the NOW(), even if you have more than 20 queries in a single transaction using it always going to be the same, or will it change?

推荐答案

显然不能保证在整个事务中,但可以随语句的不同而变化.有一种解决方法,您可以使用如下所示:

Apparently it is not guaranteed across a transaction but can change from statement to statement. There is a workaround you can use as shown here:

BEGIN;
SELECT @now := NOW();
INSERT ... VALUES (..., @now, ...);
INSERT ... VALUES (..., @now, ...);
UPDATE ... @now ...;
COMMIT; 

如果要完全避免这种情况,只需将当前日期和时间设置为PHP变量,然后改用该变量即可.

If you want to avoid that altogether just set the current date and time into a PHP variable and use that instead.

这篇关于NOW()是否可以保证DATETIME InnoDB事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆