在 PostgreSQL 中检查日期是否超过 3 年

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10258680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:50:35  来源:igfitidea点击:

Checking if date is more than 3 years old in PostgreSQL

postgresqldate

提问by Danny Calladine

At the moment I have a table that contains 4 dates:

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

CREATE TABLE dates (
date date
);

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

I would like to have a statement where I can find all dates that are older than three years old. That should be the first 2 dates.

我想有一个声明,我可以在其中找到所有超过三年的日期。那应该是前两个日期。

So far I have:

到目前为止,我有:

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

but this doesn't work.

但这不起作用。

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by vyegorov

Try this:

试试这个:

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

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

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