SQL查询,执行顺序

发布于 2024-08-05 12:29:54 字数 72 浏览 4 评论 0原文

如果查询同时包含 group by 和 order by 子句,那么 SQL 的执行顺序是什么?这取决于它们在查询中的位置吗???

What will be the sequence of execution followed by SQL if a query has both group by and order by clause. Does it depend on their position in the query???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怎言笑 2024-08-12 12:29:54

ORDER BY 始终对GROUP BY 执行的分组结果执行,即始终“after”。在标准 SQL 中,您必须在 GROUP BY 词法上将 ORDER BY 放在 GROUP BY 后面(如果两者都存在),以“提醒您”这一事实。

ORDER BY always executes on the results of the grouping performed by GROUP BY, i.e., always "after". In standard SQL, you must have ORDER BY lexically after GROUP BY, if both are present, to kind of "remind you" of the fact.

柠北森屋 2024-08-12 12:29:54

为了:

来自 & JOIN确定并确定过滤行
WHERE 行上有更多过滤器
GROUP BY 将这些行组合成组
HAVING 过滤器组
ORDER BY 排列剩余的行/组

in order:

FROM & JOINs determine & filter rows
WHERE more filters on the rows
GROUP BY combines those rows into groups
HAVING filters groups
ORDER BY arranges the remaining rows/groups

姜生凉生 2024-08-12 12:29:54

这取决于很多因素,包括您正在使用的 RDMS。了解正在发生的情况的最佳方法是使用允许您查看查询执行计划的查询工具。

It depends on many things including the RDMS you are using. The best way to find out what is going on is to use a query tool that allows you to see the query execution plan.

似狗非友 2024-08-12 12:29:54

排序依据通常发生在最后。

如果您使用的是 SQL Server,请启动查询分析器和执行计划 将为您提供查询的良好图形表示。

Order by generally happens last.

If you're using SQL Server, fire up query analyzer and execution plan will give you a nice graphical representation of your query.

寻梦旅人 2024-08-12 12:29:54

任何 SQL 语句都没有强制执行顺序。要求的结果是与“规范”评估获得的结果相匹配。在规范计算中,ORDER BY 最后应用(即使在计算 SELECT 列表表达式之后),但这并不意味着排序会推迟到实际系统上实际执行查询时的那个点。

The sequence of execution is not mandated by any SQL statement. What is mandated is that the result match the result that would be obtained by a "canonical" evaluation. In the canonical evaluation, the ORDER BY is applied last (even after the SELECT list expressions are evaluated), but that doesn't mean sorting is postponed to that point in the actual execution of a query on a real system.

从﹋此江山别 2024-08-12 12:29:54

group by 首先执行,然后对分组的结果进行排序。

group by gets executed first and then the results of the group are ordered.

不一样的天空 2024-08-12 12:29:54

假设我们有一个 SQL 查询:

SELECT   ...
  FROM     ...
  WHERE    ...
  GROUP BY ...
  HAVING   ...
  ORDER BY ...

SQL 查询子句的执行顺序是:

 1. FROM clause
 2. WHERE clause
 3. GROUP BY clause
 4. HAVING clause
 5. SELECT clause
 6. ORDER BY clause

Let's assume we have SQL query:

SELECT   ...
  FROM     ...
  WHERE    ...
  GROUP BY ...
  HAVING   ...
  ORDER BY ...

the order in which sub-clauses of SQL query are executed is:

 1. FROM clause
 2. WHERE clause
 3. GROUP BY clause
 4. HAVING clause
 5. SELECT clause
 6. ORDER BY clause
近箐 2024-08-12 12:29:54

我还建议对特定的数据库引擎使用查询分析器。下面是 Postgres 中的一个示例,它解释了 ORDER BY 首先执行,然后是 WHERE 过滤器:

EXPLAIN
SELECT * FROM table WHERE id=x AND date<='yyyy-mm-dd' ORDER BY date DESC;

因此,如果我将 DESC 更改为 ASC,结果集将包含不同的记录!

I will also suggest using a query analyzer for the specific database engine. The following is an example in Postgres, which explains that ORDER BY is executed first and after that the WHERE filter:

EXPLAIN
SELECT * FROM table WHERE id=x AND date<='yyyy-mm-dd' ORDER BY date DESC;

So if I alter DESC to ASC the result set will contain different records!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文