我正在写一个查询,你在一张 table 上有票。列 first_use_on 表示第一次使用的 TIMESTAMP。 first_use_on 默认为 NULL,然后仅在第一次使用时更新一次。我的查询工作正常,但现在我需要在查询之外知道运行的查询是否触发了 first_use_on,所以我考虑添加 first_use_on = NOW() AS is_first_usage
在返回。我可以 100% 确定返回中比较的 NOW() 始终与 UPDATE
中使用的相同吗?部分?在某些情况下它们会有所不同吗?
UPDATE
l_codes
SET first_use_on = (
CASE WHEN first_use_on IS NULL THEN
NOW()
ELSE
first_use_on
END )
WHERE
l_code = 'C9TCH' AND id_mostra = 1
RETURNING
first_use_on,
first_use_on = NOW() AS is_first_usage,
NOW() > DATE_TRUNC('day', first_use_on + INTERVAL '1 DAY') AS expired,
DATE_TRUNC('day', first_use_on + INTERVAL '1 DAY') AS expiration_on;
最佳答案
是的。如 now()
的文档中所述:
Since these functions return the start time of the current transaction, their values do not change during the transaction. This is considered a feature: the intent is to allow a single transaction to have a consistent notion of the “current” time, so that multiple modifications within the same transaction bear the same time stamp.
整个语句的事务开始是不变的。
关于sql - Postgres : is NOW() used in more places of the query guaranteed to be always the same?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62515936/