在MySQL中执行子句的预定义顺序是什么?其中一些是在运行时决定的,这个顺序正确吗?
FROM clause
-
WHERE clause
GROUP BY clause
-
HAVING clause
-
SELECT clause
ORDER BY clause
在MySQL中执行子句的预定义顺序是什么?其中一些是在运行时决定的,这个顺序正确吗?
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause
ORDER BY clause
MySQL语句的实际执行有点棘手.然而,该标准确实规定了查询中元素的解释顺序.这基本上是按照您指定的顺序进行的,尽管我认为HAVING
和GROUP BY
可能在SELECT
之后:
FROM
条款WHERE
条款SELECT
条款GROUP BY
条款HAVING
条款ORDER BY
条款这对于理解如何解析查询很重要.例如,不能使用WHERE
子句中SELECT
中定义的列别名,因为WHERE
是在SELECT
之前解析的.另一方面,这样的别名可以出现在ORDER BY
子句中.
至于实际执行,这实际上是由优化器决定的.例如:
. . .
GROUP BY a, b, c
ORDER BY NULL
和
. . .
GROUP BY a, b, c
ORDER BY a, b, c
both have the effect of the ORDER BY
not being executed at all -- 和 so not executed after the GROUP BY
(in the first case, the effect is to remove sorting from the GROUP BY
和 in the second the effect is to do nothing more than the GROUP BY
already does).