数据库中的表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.month12
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 )以上方法,看起来对,但做起来却为什么没有对呢?请帮忙!

解决方案 »

  1.   

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

  2.   

    用plsql执行后,显示错误“全年度值必须介于-4713到+9999之间”
    什么原因呢?
      

  3.   

    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 )
    这个应该是错的,因为只要满足select * from b2 where b2.ID = b1.ID and add_months(b1.time1, b2.time2) < sysdate 的表中存在数据就会将b1表中所有的数据输出,不论产品有没有过期。
    由于我对于add_months()函数不大了解,所以,后面的不大清楚
      

  4.   

    我建议将sysdate转换成char型,用to_char(add_months(b1.time1, b2.time2),'yyyy-mm-dd')<to_char(sysdate,'yyyy-mm-dd')作为条件试一下
      

  5.   

    select
      ti.*
    from
      b1 t1,
      b2 t2
    where
      t2.id = t1.id
      and add_months(t1.time1, nvl(t2.time2, 0)) > sysdate;
      

  6.   

    select b1.id,b1.time1
    from b1,b2
    where b1.id=b2.id and
          sysdate-b1.time1>b2.time2;