Oracle union all和order by一起使用

来源:互联网 发布:网络剧校花的贴身高手2 编辑:程序博客网 时间:2024/06/06 04:37
有时候,我们会将进过排序(order by)后的结果集与其他经过排序的结果集进行合并(union or union all)     
比如:     
select * from tb where length(id)=5 order by id desc     
union all     
select * from tb where length(id)=10 order by id asc     

通常情况下,上面的查询将会得到下面的错误提示:ORA-00933: SQL command not properly ended  错误指向union关键字这里     

解决办法:  
select * from (     
       select col_1,col_2,col_3,status     
       from t     
       where status >= 0     
       order by status)     
union all     
select * from (     
       select col_1,col_2,col_3,status     
       from t     
       where status < 0     
       order by status)   

   

     

原创粉丝点击