数据库中的表b1与b2分别包含两个字段:生产日期time1(data类型)与保持期time2(单位为月分,数据类型为number型) 
现在的需求是,我要搜索已经过期的产品,所以要用当前的时间减去time1,然后再跟time2比较,如果比time2大,说明已经过期。 
b1与b2都有个字段id是关联的。
假设查询的是产品的id,存在于表b1中,谁帮我写出这个sql语句?
我的数据库是oracle

解决方案 »

  1.   

    select b.id as id from ljftest2 b,(SELECT MONTHS_BETWEEN(to_date('2007-10-01','yy-mm-dd'), producedate) as month1
      FROM ljftest1) a where b.bzmonth >= a.month1
      

  2.   

    select b1.ID, b1.time1
    from b1 
    where exists (select * from b2 where b2.ID = b1.ID and add_months(b1.time1, b2.time2) < sysdate )
      

  3.   

    select b1.id, b1.time1
    from b1,b2
    where b1.id=b2.id and add_months(b1.time1, b2.time2) < sysdate )
      

  4.   

    select b1.id
      from b1,b2
     where b1.id = b2.id
       and add_months(b1.time1,b2.time2) < sysdate;试试看~~