我有张表里面有很多记录,但是我只要获取三个月之前的记录,该表里面没有日期这一列,但是我可以通过PK去其他表里面找到日期这列,然后对日期进行运算。
select * from table1 where id in (select id from table2 where sysdate-date<90)
但是说select id from table2 where sysdate-date<90里面有多行数据。
我想请问一下,我的SQL语句是有问题的。因为sysdate-date<90这个条件选出来的日期就不对
所以我想请问一下,这个条件语句筛选三个月之前的数据时间怎么写?谢谢!

解决方案 »

  1.   

    google下,或者百度一下,查查sql里的datediff函数
      

  2.   

    我的是ORALCE数据库,我用了DATEDIFF,显示invalid identifier
      

  3.   

    date < sysdate - 90
      

  4.   

    select id from table2 where sysdate-date >90
    问题解决了。呵呵。应该上面那么写。
      

  5.   

    sysdate-date>90
    这种写法本身就有问题,会影响效率的,至少应该这样写,date<sysdate-90
    oracle有自带的函数 add_month可以用,就像4楼写的那样。add_month(sysdate,-3)这个就是三个月前的,所以可以这样写:
    select * from table1 where id in (select id from table2 where date<add_month(sysdate,-3))