一.合并后排序
1.UNION/UNION ALL排序时,无论有多少个被union/union all的部分,只有最后一个union/union all部分才能拥有一个order by子句
2.UNION/UNION ALL的order by子句只能通过列号或别名来标示你要排序的字段
例: select t1.account_id account_id,t1.account_name account_name from t1
union/union all
select t2.account_id account_id,t2.account_name account_name from t2
union/union all
select t3.account_id account_id,t3.account_name account_name from t3
order by 1/account_id;
注:"/"是或的意思,不代表是语法或具有含义。
二.排序后合并
1.多个结果集在被union/union all连接后仍然保持原有的排序,需要把结果集放在子查询中
例: select * from
(select t1.account_id account_id,t1.account_name account_name from t1 order by t1.account_id)
union/union all
select * from
(select t2.account_id account_id,t2.account_name account_name from t2 order by t2.account_id)
union/union all
select * from
(select t3.account_id account_id,t3.account_name account_name from t3 order by t3.account_id)
;
注:"/"是或的意思,不代表是语法或具有含义。