UNION ALL 是否保证结果集的顺序 [英] Does UNION ALL guarantee the order of the result set
本文介绍了UNION ALL 是否保证结果集的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以确定以下脚本的结果集总是像这样 O-R-D-E-R 排序吗?
Can I be sure that the result set of the following script will always be sorted like this O-R-D-E-R ?
SELECT 'O'
UNION ALL
SELECT 'R'
UNION ALL
SELECT 'D'
UNION ALL
SELECT 'E'
UNION ALL
SELECT 'R'
能否证明有时顺序不同?
Can it be proved to sometimes be in a different order?
推荐答案
没有固有的顺序,必须使用ORDER BY
.对于您的示例,您可以通过将 SortOrder
添加到每个 SELECT 来轻松完成此操作.这将按照您想要的顺序保存记录:
There is no inherent order, you have to use ORDER BY
. For your example you can easily do this by adding a SortOrder
to each SELECT. This will then keep the records in the order you want:
SELECT 'O', 1 SortOrder
UNION ALL
SELECT 'R', 2
UNION ALL
SELECT 'D', 3
UNION ALL
SELECT 'E', 4
UNION ALL
SELECT 'R', 5
ORDER BY SortOrder
除非您在查询中特别提供订单,否则您无法保证订单.
You cannot guarantee the order unless you specifically provide an order by with the query.
这篇关于UNION ALL 是否保证结果集的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文