我想根据两个表的ROWNUM进行外联结,最好只用一条SQL语句实现。比如有A,B两表,当A表的记录数比B表多时,达到A=B(+)的效果,当A表的记录数比B表少时达到A(+)=B的效果。

解决方案 »

  1.   

    select a_id,... from 
      ( select a_id,b_id,... from 
         (select a.rownum as a_id,... from a ) as A1,
         (select b.rownum as b_id,... from a ) as B1
       where A1.a_id = B1.b_id(+)
      )union allselect b_id,... from 
      ( select a_id,b_id,... from 
         (select a.rownum as a_id,... from a ) as A1,
         (select b.rownum as b_id,... from a ) as B1
       where B1.b_id = A1.a_id(+)
       and not exists 
         (  select 1 from
             ( select a_id,b_id,... from 
                 (select a.rownum as a_id,... from a ) as A1,
                  (select b.rownum as b_id,... from a ) as B1
               where A1.a_id = B1.b_id(+)
              ) XX where XX.a_id = B1.b_id
          )
      )