如何选择12个月以上的数据? [英] How to select data that is older than 12 months?
问题描述
我有一张桌子,如下:
caravan_id, description, date_purchased, special_instructions, health&safety_check, notes
health&safety_check
是日期字段.
如何对该表中的数据执行SQL查询,以返回health&safety_check
早于12个月的所有行.但是,如果可能的话,则以12个月为基础,而不是以今天的日期为基础,例如< 2011年10月12日
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. But if possible one that is based on 12 months, not on today's date such as < 10/12/2011
推荐答案
类似的东西
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在评论中提出了一个很好的观点(尽管在Oracle中,您可以在函数上创建索引,以便解决问题).这很容易解决:
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:
where "health&safety_check" <add_months(sysdate, -12)
这篇关于如何选择12个月以上的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!