2017-03-09 18 views
2

2つのクエリがあります。どちらも1つの結合条件でのみ異なります。 1つのクエリにする方法はありますか。同じ種類の結合を持つ2つのクエリを結合する

最初のクエリ:

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’ 
); 

2のクエリ:

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です。

答えて

1

は多分サブでUNIONを使用し、事前に

おかげでidとID1のために選択します。

select id from table1 
union 
select id1 from table1 
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’ 
); 
0

何を?

where EXISTS (
     select 1 from tmp 
     where TASKSUB.PK1 = tmp.id or TASKSUB.PK1 = tmp.id1 
    ) and 
    TASKSUB.REF= ‘Shopping’ 
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’ 
) 
関連する問題