Oracle中union all、union 和order by一起使用的解決方法

當union all和order by 如下一起使用時,會報sql未正確結束

select name,age from student order by age

union all

select name,age from person order by age

通用的解決方法有三種:

1、將結果集作爲一張臨時表然後查詢排序

select * from (select name,age from student order by age

union all

select name,age from person order by age) order by age

2、單獨對錶進行排序後進行並集操作

select * from (select name,age from student order by age)

union all

select * from (select name,age from person order by age)

3、order by + 字段在結果集中的序號

select name,age from student 

union all

select name,age from person 

order by 2

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章