Does table1 UNION ALL table2 guarantee output order table1, table2?

匆匆过客 提交于 2019-12-11 00:27:50

问题


SELECT a FROM b
UNION ALL 
SELECT a FROM c
UNION ALL 
SELECT a FROM d

Does UNION ALL guarantee to print out records from tables b, c, d in that order? I.e., no records from c before any from b. This question is not for a specific DBMS.


回答1:


No order by, no order guarantee whatsoever - that's for every database.

And for standard SQL, an ORDER BY is applied to the results from all the unioned queries.




回答2:


To be sure in order use

Select 1 as TableNo,* from a
union all 
select 2 as TableNo,* from b
union all
select 3 as TableNO,* from c
order by TableNo, [desired column]


来源:https://stackoverflow.com/questions/3473419/does-table1-union-all-table2-guarantee-output-order-table1-table2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!