是否保证子查询中的顺序得以保留? [英] Is order in a subquery guaranteed to be preserved?

查看:99
本文介绍了是否保证子查询中的顺序得以保留?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特别想知道PostgreSQL。给出以下人为设计的示例:

I am wondering in particular about PostgreSQL. Given the following contrived example:

SELECT name FROM
  (SELECT name FROM people WHERE age >= 18 ORDER BY age DESC) p
LIMIT 10

从外部查询返回的名称是否保证为

Are the names returned from the outer query guaranteed to be be in the order they were for the inner query?

推荐答案

否,在外部查询中按以下顺序排列:

No, put the order by in the outer query:

SELECT name FROM
  (SELECT name, age FROM people WHERE age >= 18) p
ORDER BY p.age DESC
LIMIT 10

内部(子)查询返回结果集。如果按此顺序进行排序,则保证从内部(子)查询传递到外部查询的中间结果集将按照您指定的方式进行排序,但在外部查询中不按结果进行排序通过处理内部查询结果集生成的结果集不保证以任何方式进行排序。

The inner (sub) query returns a result-set. If you put the order by there, then the intermediate result-set passed from the inner (sub) query, to the outer query, is guaranteed to be ordered the way you designate, but without an order by in the outer query, the result-set generated by processing that inner query result-set, is not guaranteed to be sorted in any way.

这篇关于是否保证子查询中的顺序得以保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆