我有一张桌子,如下:
caravan_id, description, date_purchased, special_instructions, health&safety_check, noteshealth&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个月以上的数据?
发布评论