简体   繁体   English

如何选择12个月以上的数据?

[英]How to select data that is older than 12 months?

I have a table as follows; 我有一张桌子,如下:

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

health&safety_check is 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_check older than 12 months. 如何对该表中的数据执行SQL查询,该查询将返回所有health&safety_check早于12个月的行。 But if possible one that is based on 12 months, not on today's date such as < 10/12/2011 但是,如果可能,则以12个月为基础,而不是以今天的日期为基础,例如<10/12/2011

Something like: 就像是:

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

I assume you don't want to hardcode the 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). Jeff在评论中提出了一个要点(尽管在Oracle中,您可以在函数上创建索引,以便解决问题)。 This is easily fixed: 这很容易解决:

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

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(),间隔-12个月)表示此日期减去12个月,您可以使用其他日期字段或变量来更改NOW()。

NOW() mean today. NOW()表示今天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM