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

Replace union all between three tables as join

我是 oracle 的新手,这可能是 simple.But 我想检查下面的 union all 是否可以用某种连接替换。

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

提前致谢

您应该使用 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 . . .
             );

注意:如果 bc 中有多个匹配项,您的原始查询将 return 复制 a 中的行。此版本没有 return 此类重复项。通常,不 return 复制是期望的行为。

届时请查看是否对您有帮助

Select a.col1
     , a.col2
     , a.col3
from a
left
join b on a.col3=b.col3 and a.col4 = b.col4 and b.col5 = -- filter cindition
left
join c on c.col3 = b.col3 and c.col4 = b.col4 and c.col5 = filter condition and c.col6 = -- extra conditions