PostgreSQL查询的日期大于3年 [英] PostgreSQL Query WHERE date is greater than 3 years old

查看:130
本文介绍了PostgreSQL查询的日期大于3年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4个日期的表:

I have a table that contains 4 dates:

CREATE TABLE dates (
    date date
);

    11-mar-2008
    25-now-2007
    18-apr-2012
    20-apr-2012

我需要一个查询,该查询返回所有早于三年的日期.那应该是前两个日期.

I need a query that returns all dates that are older than three-years-old. That should be the first 2 dates.

这对我不起作用:

SELECT * FROM dates WHERE date = now()::-1095;

推荐答案

SELECT 
  * 
FROM 
  dates 
WHERE 
  date < now() - '3 years' :: interval;

此外,命名列 date 也不是一个好习惯,因为这是PostgreSQL中的保留字.

Also, naming your column date is not a good practice, as this is a reserved word in PostgreSQL.

这篇关于PostgreSQL查询的日期大于3年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆