select a,
       b,
       c,
       d
  from tbl_x x, tbl_y y
 where x.a = y.a 
Union all
select a,
       b,
       b,
       d
  from tbl_t1 t1, tbl_t2 t2
 where t1.a = t2.a(+)
    
这样是可以的select a,
       b,
       c,
       d
  from tbl_x x, tbl_y y
 where x.a = y.a 
Union all
select a,
       b,
       b,
       d
  from tbl_t1 t1, tbl_t2 t2
 where t1.a = t2.a(+)
   order by t1.a
 
这样是不行的?这是为何呢?

解决方案 »

  1.   

    select a, 
          b, 
          c, 
          d 
      from tbl_x x, tbl_y y 
    where x.a = y.a 
    Union all 
    select a, 
          b, 
          b, 
          d 
      from tbl_t1 t1, tbl_t2 t2 
    where t1.a = t2.a(+) 
      order by t1.a,
                b,c,d
    oracle 排序是要将你查询的列全部列出,计算列可以除外
      

  2.   

    改成order by a应该就没问题
    不能指定t1.a,因为union all合并了a字段,这里面不单单是t1.a,所以指定t1.a会报错。
     
      

  3.   

    oh,应当是我没有表达清楚意思,我期待的效果是(select a, 
          b, 
          c, 
          d 
      from tbl_x x, tbl_y y 
    where x.a = y.a) 
    Union all (
    select a, 
          b, 
          b, 
          d 
      from tbl_t1 t1, tbl_t2 t2 
    where t1.a = t2.a(+) 
      order by t1.a)但是打了括号也不行啊? 
      

  4.   

    可以试试在每个子句上加order by 然后联合,或者
    select a,b,c,d from (
    select a,
          b,
          c,
          d
      from tbl_x x, tbl_y y
    where x.a = y.a
    Union all
    select a,
          b,
          b,
          d
      from tbl_t1 t1, tbl_t2 t2
    where t1.a = t2.a(+))
      order by a 
      

  5.   

    可以加ORDER BY吧....
    将Order by 放在最后
      

  6.   

    再嵌套一级吧
    select ...
    union all
    select * from (select ... order by ...)