SQL 如何列出过去 10 天的日期记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5465484/
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
How to list records with date from the last 10 days?
提问by nunos
SELECT Table.date FROM Table WHERE date > current_date - 10;
Does this work on PostgreSQL?
这适用于 PostgreSQL 吗?
回答by a_horse_with_no_name
Yes this does work in PostgreSQL (assuming the column "date" is of datatype date
)
Why don't you just try it?
是的,这在 PostgreSQL 中确实有效(假设列“日期”是数据类型date
)为什么不试试呢?
The standard ANSI SQL format would be:
标准的 ANSI SQL 格式为:
SELECT Table.date
FROM Table
WHERE date > current_date - interval '10' day;
I prefer that format as it makes things easier to read (but it is the same as current_date - 10
).
我更喜欢这种格式,因为它使事情更容易阅读(但它与 相同current_date - 10
)。
回答by bradley.ayers
http://www.postgresql.org/docs/current/static/functions-datetime.htmlshows operators you can use for working with dates and times (and intervals).
http://www.postgresql.org/docs/current/static/functions-datetime.html显示了可用于处理日期和时间(和间隔)的运算符。
So you want
所以你要
SELECT "date"
FROM "Table"
WHERE "date" > (CURRENT_DATE - INTERVAL '10 days');
The operators/functions above are documented in detail:
上面的运算符/函数有详细记录:
回答by Highly Irregular
My understanding from my testing (and the PostgreSQL dox) is that the quotes need to be done differently from the other answers, and should also include "day" like this:
我从我的测试(和PostgreSQL dox)中的理解是,引号需要与其他答案不同,并且还应该包括“day”,如下所示:
SELECT Table.date
FROM Table
WHERE date > current_date - interval '10 day';
Demonstrated here (you should be able to run this on any Postgres db):
在这里演示(您应该能够在任何 Postgres 数据库上运行它):
SELECT DISTINCT current_date,
current_date - interval '10' day,
current_date - interval '10 days'
FROM pg_language;
Result:
结果:
2013-03-01 2013-03-01 00:00:00 2013-02-19 00:00:00
回答by Hymans Nogueira
you can use between too:
你也可以在两者之间使用:
SELECT Table.date
FROM Table
WHERE date between current_date and current_date - interval '10 day';
回答by Scherbius.com
I would check datatypes.
我会检查数据类型。
current_date has "date" datatype, 10 is a number, and Table.date - you need to look at your table.
current_date 具有“日期”数据类型,10 是一个数字,而 Table.date - 您需要查看您的表格。