0

在这里,我有两个名为main_tableand的表main_table_replicamain_table_replica是 的复制品main_table。但问题是我可以在main_table或中找到数据main_table_replica。现在我正在做如下查询。我怎样才能避免在这里联合?在这两个查询中,唯一的变化是main_tableand main_table_replica

SELECT DISTINCT e.some_id
         FROM
             main_table e,   //Change is here
             main_table_join_one x     
             where  e.some_id = x.some_id(+)
             and (x.status in('A','I') or x.status is null)
             and e.code='XYZ' and e.second_code in('XYZ','ABC')


             UNION

        SELECT DISTINCT t.some_id
         FROM
             main_table_replica t,   //Change is here
             main_table_join_one xf     
             where  t.some_id = xf.some_id(+)
             and (xf.status in('A','I') or xf.status is null)
             and t.code='XYZ' and t.second_code in('XYZ','ABC')  

谢谢!

4

1 回答 1

2

以这种方式从两个不同的表中获取东西正是union它的用途。没有理由避免它。

但是,您可以缩小联合的范围以减少重复:

select distinct combined.some_id from (
        select e.some_id from main_table e
            union
        select t.some_id from main_table_replica t
    ) combined
    inner join  main_table_join_one x on
        combined.some_id = x.some_id(+) and
        (x.status in('A','I') or x.status is null) and
        combined.code='XYZ' and 
        combined.second_code in('XYZ','ABC');
于 2013-03-06T15:13:50.730 回答