oracle查询:同一个表选择了两次,具有不同的别名但相同的where子句过滤器

时间:2012-07-26 18:45:45

标签: oracle11g

我有一个这样的查询,我应该重构:

SELECT *
  FROM a a1
      ,b common_alias
      ,b common_alias1
 WHERE a1.id = common_alias.id(+)
   AND a1.id = common_alias1.id(+)
   AND common_alias.name = 'XYZ'
   AND common_alias1.name = 'XYZ'
UNION
SELECT *
  FROM a a1
      ,b common_alias
      ,b common_alias1
 WHERE a1.id = common_alias.id(+)
   AND a1.id = common_alias1.id(+)
   AND common_alias.name = 'XYZ'
   AND common_alias1.name = 'PQR'

在我看来,我可以完全删除第一个查询。是否存在删除第一个查询会产生影响的情况?

1 个答案:

答案 0 :(得分:1)

您应该审核common_alias1.id上的外部联接条件,它没有任何效果,因为您也不会在common_alias1.name上进行外部联接。请参阅Oracle outer join with filter condition on the second table

但回到你的问题:不,你不能删除第一部分,你会在哪里获得XYZ | XYZ行?有关示例,请参阅http://sqlfiddle.com/#!4/c7dfa/12

但你可以重构

SELECT a1.*,common_alias.name n1,common_alias1.name n2
  FROM a a1
      ,b common_alias
      ,b common_alias1
 WHERE a1.id = common_alias.id(+)
   AND a1.id = common_alias1.id(+)
   AND common_alias.name = 'XYZ'
   AND (common_alias1.name = 'XYZ' OR common_alias1.name = 'PQR')
;