sql - Postgres : is NOW() used in more places of the query guaranteed to be always the same?

标签 sql postgresql date current-time

我正在写一个查询,你在一张 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/

相关文章:

mysql - SQL根据第一个表中的时间差连接第二个表

sql - 查找表中不存在的 IDS

r - 查找 R Lubridate 中无法解析的日期

java - 如何在日期解析(Java)中忽略时区标识符?

php - 每 30 秒运行一次 SQL 查询(计数),然后将输出保存到某个文件

mysql - 1 :M Recursive Relationship 上的 SQL Select 语句

sql - 向 SQL Server 用户授予执行权限以仅运行特定作业

php - 使用php编程从Mysql数据库中删除重复的主键

sql - 如何采用单独列排序的 DISTINCT ON 子查询,并使其快速?

apache-spark - Pyspark 将带有日期和月份名称的日期时间字段解析为时间戳