postgresql SQL 语句显示时间间隔之外的日期

postgresl SQL statement displays dates outside the interval

在 RHEL 上,命令提示符处的 date/time 是 UTC 时间。 我查询我的 table 最后一小时的日期,但它 returns 我其他 dates/times。我不知道它是否与时区相关,但我非常困惑如何使用它

select to_char(update_dt, 'DD Mon YYYY HH12:MI'),data 
from mytable
where update_dt > current_date - interval '1' hour


      to_char      |   data
-------------------+----------------
 07 May 2020 12:37 | blah
 07 May 2020 12:37 | blah
 07 May 2020 12:37 | blah
 07 May 2020 12:37 | blah
 07 May 2020 12:37 | blah
 07 May 2020 05:23 | huh
 07 May 2020 05:23 | huh
 07 May 2020 05:22 | huh

[root@ip-172-31-1-28 ~]# date
Thu May  7 13:25:03 UTC 2020

这个表达式:

where update_dt > current_date - interval '1' hour

从昨天午夜开始,因为 current_date 只是 日期 的午夜(日期的开始)。

您似乎想要时间包括:

where update_dt > now() - interval '1' hour

您还可以使用:

where update_dt > current_timestamp - interval '1' hour

但是 now() 更容易打字。