员工表 t_emp工号 姓名 离职日期
001 张三 Null (空表示未离职)
002 李四 2006.11.10
003 王五 NUll
004 赵六 2006.12.20
.....加班表 t_over工号 加班日期
001 2006.11.15
002 2006.11.09 //这行保留
002 2006.11.15 //要删除这行
002 2006.11.16 //要删除这行
003 2006.11.15
004 2006.11.15
.....现想将2006.11月离职后有加班的人的加班记录删除,
能否用一句SQL完成?否则要用游标循环员工表,再单笔删除加班表的记录。多谢!

解决方案 »

  1.   

    delete from t_over 
    where 加班日期>=2006.11.01
    and 工号 in(
    select 工号 from t_emp where 离职日期>=2006.11.01
    )--語法基本是這樣﹐日期如果不符合自己調整一下﹐快把分給我﹗
      

  2.   

    delete from t_over t1
    where exists
    (
    select 1 from t_emp t2
    where t1.工号=t2.工号 and t2.离职日期>='2006.11.01' and t2.离职日期<='2006.11.30'
    )
      

  3.   

    delete t_over x
     where exists (select 'a'
              from t_tmp y
             where y.离职日期 is not null
               and x.工号 = y.工号
               and x.加班日期> y.离职日期)
      

  4.   

    DELETE FROM t_over
          WHERE (工号, 加班日期) IN (SELECT 工号, 加班日期
                                       FROM t_over a, t_emp b
                                      WHERE a.工号 = b.工号(+) and a.离职日期 < b.加班日期)
      

  5.   

    delete t_over x
     where exists (select 'a'
              from t_tmp y
             where y.离职日期 is not null
               and x.工号 = y.工号
               and x.加班日期> y.离职日期)强.效率最高,最灵活.