union all中使用order by子查詢時需將含order by的子查詢包在一個不含order by的查詢裡再進行union all。

否則會報“Incorrect usage of UNION and ORDER BY”

例如:

1.錯誤用法:

           select id from test1 order by id

           union all

           select id from test2 order by id

2.正確用法:

           select * from (select id from test1 order by id) t1

           union all

           select * from (select id from test2 order by id) t2