繁体   English   中英

postgresql中的日期比较不起作用

[英]Date comparison in postgresql not working

我的postresql数据库中的查询有问题。 我试图在特定日期时间之后获取最后100个日志,但是当我使用此查询时:

select * from log_entry WHERE array['MESSAGE'] && tags AND CAST(last_updated AS DATE) >= '2013-02-28T16:47:26.394213' ORDER BY last_updated DESC LIMIT 100

这里的last_updated列的类型为:timestamp without time zone

有时我会在'2013-02-28T16:47:26.394213'之前收到日志,我做错了什么? 有没有更好的方法来做这个而不是使用演员?

提前致谢!

(根据要求从评论中转发。)

嗯,这是你的问题:当你将时间戳(有或没有时区)转换为日期时,你会截断时间戳的时间部分。 为什么不将'2013-02-28T16:47:26.394213'到时间戳并直接与last_updated进行比较?

select * from log_entry 
WHERE array['MESSAGE'] && tags 
AND last_updated 
  >= '2013-02-28T16:47:26.394213'::timestamp without time zone
ORDER BY last_updated DESC LIMIT 100

使用它。这应该工作。

select * from log_entry WHERE array ['MESSAGE'] && tags AND age(last_updated)

= age(now())ORDER BY last_updated DESC LIMIT 100

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM