UNION ALL 是否保证结果集的顺序

编程入门 行业动态 更新时间:2024-10-27 00:30:02
本文介绍了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 是否保证结果集的顺序

本文发布于:2023-11-06 08:00:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   UNION

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!