SQL PostgreSQL,检查相对于“今天”的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3928575/
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
PostgreSQL, checking date relative to "today"
提问by Joseph
Was wondering if someone could assist with some Postgres. I have a table which has a column called mydate
which is a postgres date type. I want to do something like:
想知道是否有人可以协助一些 Postgres。我有一个表,其中有一列名为mydate
postgres 日期类型。我想做类似的事情:
SELECT * FROM MyTable WHERE mydate > [Today-1year]
I've never used Postgres before and I'm sure I just need to know the name of some functions- I'll gladly look up the reference myself. Can anyone point me in the right direction?
我以前从未使用过 Postgres,我确定我只需要知道一些函数的名称——我很乐意自己查找参考。任何人都可以指出我正确的方向吗?
Thanks!
谢谢!
回答by Paul Tomblin
select * from mytable where mydate > now() - interval '1 year';
If you only care about the date and not the time, substitute current_date
for now()
如果您只关心日期而不关心时间,请替换current_date
为now()
回答by Alex Howansky
I think this will do it:
我认为这会做到:
SELECT * FROM MyTable WHERE mydate > now()::date - 365;
回答by coderaj
This should give you the current date minus 1 year:
这应该给你当前日期减去 1 年:
select now() - interval '1 year';
回答by Doc
You could also check using the age()
function
您还可以使用该age()
功能进行检查
select * from mytable where age( mydate, now() ) > '1 year';
select * from mytable where age( mydate, now() ) > '1 year';
age()
wil return an interval.
age()
将返回一个区间。
For example age( '2015-09-22', now() )
will return -1 years -7 days -10:56:18.274131
例如age( '2015-09-22', now() )
将返回-1 years -7 days -10:56:18.274131