6
4

1 回答 1

10

你误读了声明。当使用多个变量时,它与 SELECT 列表中的表达式顺序有关。
如前所述,这个单变量语句上的 ORDER BY 有保证的顺序直到 MySQL 的当前版本,并且该文本中没有任何内容表明它会改变。

保证未来?谁知道。


关于中断查询,您再次误解了 MySQL 的工作原理。让我们分解您的查询。请注意手册中的此声明

在 SELECT 语句中,每个选择表达式仅在发送到客户端时才被计算。这意味着在 HAVING、GROUP BY 或 ORDER BY 子句中,引用在选择表达式列表中分配了值的变量不会按预期工作

查询的处理顺序大致是

FROM / JOIN
WHERE / ON
GROUP BY / ROLLUP
HAVING
UNION
SELECT
ORDER BY
@variable resolution

Your "broken" query attempts to use the variable WITHIN the same level, which is just about as sinful as using a WHERE/HAVING clause against a column alias. That's why you'll never see MySQL variable-based row_numbering solutions using the variable on the same query-level, it is always in a subquery. The outer query can be considered the client of the inner query at which stage the variable/placeholder-expression has been rendered. By your argument, you can just as easily break it using a WHERE clause involving the @row directly (yes it will run!).

于 2012-10-04T14:05:06.193 回答