oracle 如何选择超过 12 个月的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13807132/
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-09-19 01:19:15  来源:igfitidea点击:

How to select data that is older than 12 months?

sqloracle

提问by Stacker-flow

I have a table as follows;

我有一张表如下;

caravan_id, description, date_purchased, special_instructions, health&safety_check, notes

health&safety_checkis a date field.

health&safety_check是一个日期字段。

How can I perform an SQL query for data in this table that returns all rows which have a health&safety_checkolder than 12 months. But if possible one that is based on 12 months, not on today's date such as < 10/12/2011

如何对该表中的数据执行 SQL 查询,以返回health&safety_check超过 12 个月的所有行。但如果可能的话,基于 12 个月的日期,而不是今天的日期,例如 < 10/12/2011

回答by Gordon Linoff

Something like:

就像是:

select *
from t
where add_months("health&safety_check", 12) < sysdate

I assume you don't want to hardcodethe current date but are happy with using a variable for it.

我假设您不想对当前日期进行硬编码,但对使用变量感到满意。

Jeff, in the comments, makes a good point (although, in Oracle, you can create an index on a function so you can get around the problem). This is easily fixed:

Jeff 在评论中提出了一个很好的观点(尽管在 Oracle 中,您可以在函数上创建索引,以便解决问题)。这很容易解决:

where "health&safety_check" <add_months(sysdate, -12)

回答by Jeffrey Kemp

select *
from t
where "health&safety_check" < add_months(sysdate, -12)

回答by Reinstar

You can use this query:

您可以使用此查询:

SELECT *
FROM table_name
WHERE `health&safety_check` < ADDDATE(NOW(), INTERVAL -12 MONTH)

ADDDATE(NOW(), INTERVAL -12 MONTH) mean this date minus 12 Month, you can use other date field or variable to change NOW().

ADDDATE(NOW(), INTERVAL -12 MONTH) 表示该日期减去 12 个月,您可以使用其他日期字段或变量来更改 NOW()。

NOW() mean today.

NOW() 表示今天。