子查询中是否允许 order by 子句 [英] Is order by clause allowed in a subquery

查看:25
本文介绍了子查询中是否允许 order by 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么理由为什么或为什么不应该在子查询中执行order by"?

Is there any reason why or why not you should do an 'order by' in a subquery?

推荐答案

是的:不应该这样做,因为它在概念上没有意义.

Yes: It should not be done, because it does not make sense conceptually.

子查询将用于某些外部查询(否则它将毫无意义),并且该外部查询无论如何都必须进行排序,因此对子查询进行排序是没有意义的.

The subquery will be used in some outer query (otherwise it would be pointless), and that outer query will have to do ordering anyway, so there's no point ordering the subquery.

这是因为 SQL 中的查询结果不会以特定顺序出现,除非您使用显式 ORDER.因此,即使您在子查询中使用了 ORDER,也不能保证这会影响外部查询的结果顺序;所以没意义.

This is because query results in SQL will come in no particular order, unless you use an explicit ORDER. So even if you used ORDER in the subquery, you have no guarantee that this will affect the order of the results from the outer query; so it's pointless.

当然,由于它的实现,它可能会对某些特定的 RDBMS 产生影响,但这将是特定于实现的,而不是您应该依赖的东西.

It may of course make a difference in some specific RDBMS because of its implementation, but that will be implementation-specific, and not something you should rely on.

当然,如果您在子查询中使用 TOP 或 LIMIT,您需要使用 ORDER.但这无论如何都不是标准的 SQL...

Of course, if you use TOP or LIMIT in the subquery, you will need to use ORDER. But that's not standard SQL anyway...

这篇关于子查询中是否允许 order by 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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