为什么该查询没有按我预期的那样工作?

问题描述:

SELECT 3 AS x, 5 as y, (x+y) as z

结果如我所愿;

x  y  z
3  5  8

我得到的结果;

#1054 - Unknown column 'x' in 'field list' 

SELECT 子句(和列别名)是同时计算的,没有特定的顺序.也就是说,在你想要 z 的同一点,x 和 y 列还不存在.

The SELECT clause (and column aliases) are evaluated at the same time and in no particular order. That is, at the same point you want z, the columns x and y do not exist yet.

SELECT
   (x+y) as z
FROM
    (
    SELECT 3 AS x, 5 as y
    ) t;