将三个表之间的union all替换为join

时间:2016-05-05 11:24:07

标签: oracle join union

我是oracle的新手,这可能很简单。但我想检查下面的union是否可以用某种连接替换。

select a.col1,a.col2,a.col3 from b,a
where a.col3=b.col3 and a.col4= b.col4
and b.col5= --filter conditions
union all
select a.col1,a.col2,a.col3 from c,a
where C.col3=b.col3 and c.col4= b.col4
and c.col5= --filter conditions
and c.col6= -- extra conditions

提前致谢

2 个答案:

答案 0 :(得分:0)

您应该使用join编写现有代码。简单规则:从不FROM子句中使用逗号。 始终使用明确的JOIN语法。

但是,在你的情况下,我会选择exists

select a.*
from a
where exists (select 1
              from b
              where a.col3 = b.col3 and a.col4 = b.col4 and . . .
             ) or
      exists (select 1
              from c
              where c.col3 = b.col3 and c.col4 = b.col4 and . . .
             );

注意:如果ab中有多个匹配项,原始查询将在c中返回重复的行。此版本不会返回此类副本。通常,不返回重复项是理想的行为。

答案 1 :(得分:0)

请检查它是否对您有帮助

e
相关问题