postgresl SQL 语句显示间隔之外的日期。

问题描述 投票:0回答:1

在RHEL上,命令提示符下的日期时间是以UTC时间为单位的,我查询我的表中最近一个小时的日期,但它却返回给我其他的日期时间。 我不知道这是否与时区有关,但我非常困惑如何使用它。

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
sql postgresql utc
1个回答
2
投票

这个表达式。

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() 是比较容易打的。

© www.soinside.com 2019 - 2024. All rights reserved.