加入两个具有相同类型连接的查询

时间:2017-03-09 07:08:35

标签: sql oracle

我有两个问题。两者的区别仅在于一个连接条件。有没有办法让它成为一个查询.Below是查询。

第一次查询:

delete from Employee where Employee_ID in    
  (    
    select PK from Dept    
     inner join Tran on Tran.PK = Dept.TX_ID   
        inner join TASK on TASK.TRANSINFO = TRAN.PK_ID   
        inner join TASK1 on TASK.PT_ID = TASK1.PK_ID  
           inner join TASKSUB on TASKSUB.PK_ID = TASK1.SUBJECT   
  where TASKSUB. PK1 in (select id  from tmp) and TASKSUB.REF= ‘Shopping’
  );

第二次查询:

    delete from Employee where Employee_ID in   
      (   
  select PK from Dept   
    inner join Tran on Tran.PK = Dept.TX_ID   
     inner join TASK on TASK.TRANSINFO = TRAN.PK_ID   
      inner join TASK1 on TASK.PT_ID = TASK1.PK_ID   
         inner join TASKSUB on TASKSUB.PK_ID = TASK1.SUBJECT   
  where TASKSUB. PK1 in (select id1  from tmp) and TASKSUB.REF= ‘Shopping’   
  );

唯一的区别是tmp表中的id和id1。

提前致谢

4 个答案:

答案 0 :(得分:1)

也许在id和id1的子选择中使用UNION。像

这样的东西
select id from table1 
union 
select id1 from table1

答案 1 :(得分:1)

delete from Employee where Employee_ID in    
(    
select PK from Dept    
 inner join Tran on Tran.PK = Dept.TX_ID   
    inner join TASK on TASK.TRANSINFO = TRAN.PK_ID   
    inner join TASK1 on TASK.PT_ID = TASK1.PK_ID  
       inner join TASKSUB on TASKSUB.PK_ID = TASK1.SUBJECT   
where TASKSUB. PK1 in (select id from tmp union all select id1 from tmp) and TASKSUB.REF= ‘Shopping’
)

答案 2 :(得分:0)

试试这个:

delete from Employee where Employee_ID in    
(    
    select PK from Dept    
    inner join Tran on Tran.PK = Dept.TX_ID   
    inner join TASK on TASK.TRANSINFO = TRAN.PK_ID   
    inner join TASK1 on TASK.PT_ID = TASK1.PK_ID  
    inner join TASKSUB on TASKSUB.PK_ID = TASK1.SUBJECT   
    where TASKSUB. PK1 in (select id  from tmp) and TASKSUB.REF= ‘Shopping’

    union

    select PK from Dept   
    inner join Tran on Tran.PK = Dept.TX_ID   
    inner join TASK on TASK.TRANSINFO = TRAN.PK_ID   
    inner join TASK1 on TASK.PT_ID = TASK1.PK_ID   
        inner join TASKSUB on TASKSUB.PK_ID = TASK1.SUBJECT   
    where TASKSUB. PK1 in (select id1  from tmp) and TASKSUB.REF= ‘Shopping’
  );

答案 3 :(得分:0)

这个怎么样?

where EXISTS (
        select 1 from tmp 
        where TASKSUB.PK1 = tmp.id or TASKSUB.PK1 = tmp.id1
    ) and 
    TASKSUB.REF= ‘Shopping’