postgreSQL-查询在最近特定时间之后创建的记录



我目前有一个数据库,该数据库将created作为创建的每个记录的时间戳。

我试图查询最近中午时间(12:00(之后创建的所有数据。

所以,假设数据是

id1 | at 12.30 2nd Oct
id2 | at 21.00 2st Oct
id3 | at 12.10 1st Oct

目前时间是10月3日11.59,我希望查询的答案是

id1 | at 12.30 2nd Oct
id2 | at 21.00 2st Oct

但是如果数据是

id1 | at 12.30 2nd Oct
id2 | at 21.00 2st Oct
id3 | at 12.10 3rd Oct

目前时间是10月3日13时,我希望查询的答案是

id3 | at 12.10 3rd Oct

最好的查询应该是什么?我在postgresql上运行数据库。

感谢并感谢所有的帮助。

您可以使用日期算术:

where createdat >= (date_trunc('day', now()) +
                    (case when extract(hour) from now()) >= 12 then interval '12 hour' else interval '-12 hour' end)
                   )

最新更新