Oracle sql查询避免使用UNION子句?

时间:2013-03-06 09:28:51

标签: sql oracle plsql

我是SQL新手。我正在使用Oracle10g。我有以下查询。它有3个部分,我正在做所有三个部分的UNION。     但是在每个查询中,除了连接表之外,大多数逻辑都是常见的。是否可以避免使用UNION并将所有内容放在一个块中?

    SELECT DISTINCT e.some_id
         FROM
             main_table e,
             main_table_join_one x     //only change
             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 ef.some_id
         FROM
             main_table ef,
             main_table_join_two xf  //only change
             where  ef.some_id = xf.some_id(+)
             and (xf.status in('A','I') or xf.status is null)
             and ef.code='XYZ' and ef.second_code in('XYZ','ABC')

             UNION

    SELECT DISTINCT eff.some_id
         FROM
             main_table eff,
             main_table_join_three xff   //only change
             where  eff.some_id = xff.some_id(+)
             and (xff.status in('A','I') or xff.status is null)
             and eff.code='XYZ' and eff.second_code in('XYZ','ABC')

谢谢!

4 个答案:

答案 0 :(得分:1)

你可以使用存在

select distinct e.some_id
  from main_table e
 where e.code = 'XYZ'
   and e.second_code in ('XYZ', 'ABC')
   and (exists (select 0
                  from main_table_join_one x / / only change
                 where x.some_id = e.some_id
                   and (x.status in ('A', 'I') or x.status is null)) 
    or exists
        (select 0
           from main_table_join_two x / / only change
          where x.some_id = e.some_id
            and (x.status in ('A', 'I') or x.status is null))  
    or exists
        (select 0
           from main_table_join_three x / / only change
          where x.some_id = e.some_id
            and (x.status in ('A', 'I') or x.status is null)))

EDITED

与主题入门案例

完全相同的结果
select distinct e.some_id
  from main_table e
 where e.code = 'XYZ'
   and e.second_code in ('XYZ', 'ABC')
   and (exists (select 0
                  from main_table_join_one x
                 where x.some_id = e.some_id
                   and x.status in ('A', 'I')) 
    or not exists
        (select 0 from main_table_join_one x where x.some_id = e.some_id) 
    or exists
        (select 0
           from main_table_join_two x
          where x.some_id = e.some_id
            and x.status in ('A', 'I'))
    or not exists
        (select 0 from main_table_join_two x where x.some_id = e.some_id) 
    or exists
        (select 0
           from main_table_join_three x
          where x.some_id = e.some_id
            and x.status in ('A', 'I')) 
    or not exists
        (select 0 from main_table_join_three x where x.some_id = e.some_id))

答案 1 :(得分:0)

你为什么要使用OUTER JOIN?您只选择main_table.some_id,外连接意味着无论其他表中是否存在任何匹配,您都会得到它。 (尽管Mark Ba​​nnister观察到,因为你已经管理了外连接语法,你实际上正在运行内部连接)。

如果你只是运行,那么你将得到相同的结果集(假设你纠正了外连接语法):

SELECT DISTINCT e.some_id
FROM main_table e
where e.code='XYZ' 
and e.second_code in('XYZ','ABC')

也许你在向我们展示一个经过消毒的测试用例时错误的逻辑?或者你可能不清楚你应该实施的业务规则?

答案 2 :(得分:0)

    select distinct ear.RecId,0 Recid,em.EmpCode EmpId,                 
      (em.EmpFirstName+' '+em.EmpMiddleName+' '+em.EmpLastName) as Name,                   
   dm.DeptName as Department,                  
   EAR.AttendType [AttendType],                  

  Convert(varchar,ActualDate,110)  [AttendDate],                  
   EAR.Reason,                      
   ear.StatusOfAdmin Status                      
    from Employeemaster em,                   
   DepartmentMaster dm,                  
   EmpAttenednaceRequest ear,                  
   empAttendanceRequestTransaction eart  

答案 3 :(得分:-1)

尝试此查询

 SELECT DISTINCT e.some_id
 FROM
         main_table e,
         main_table_join_one x,
         main_table_join_two xf,
         main_table_join_three xff 
 where e.code='XYZ' and e.second_code in('XYZ','ABC') AND
         ((e.some_id = x.some_id
         and (x.status in('A','I') or x.status is null) 
         OR (ef.some_id = xf.some_id
         and (xf.status in('A','I') or xf.status is null))
         OR (eff.some_id = xff.some_id
         and (xff.status in('A','I') or xff.status is null)))

如果ef和eff不存在

EDITED

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