1. 程式人生 > >union all和order by一起使用出問題

union all和order by一起使用出問題

select * from (select * from (select zxbz,count(*)rs from dc_jhmy where 1=1 group by zxbz  order by rs desc
    union all
       select 'hj' as zxbz,count(*)rs from dc_jhmy where 1=1)where rownum <= 100

SQL語句如上,我想實現除最後的union all 之前所有的記錄先排序,然後在加上最後一條記錄,可是   order by rs desc
放在現在的位置報錯,命令未正確結束。

解決方法如下:利用臨時表

select * from (select * from (select zxbz,count(*)rs from dc_jhmy where  1=1 group by zxbz  order by rs desc)
    union all
       select 'hj' as zxbz,count(*)rs from dc_jhmy where 1=1)where rownum <= 100