SELECT 中的临时变量操作可见性
temporary variable manipulation visibility in SELECT
MySQL中如何定义临时变量操作?似乎值的任何变化都会在赋值表达式之后立即 "visible" 。如果我 运行 查询
SELECT
@x as x,
@x + (@x:=@x+1) + @x as magic,
@x as new_x
FROM
(SELECT @x:=0) x,
Values1to100 numbers
它returns
+---+-------+-------+
| x | magic | new_x |
+===+=======+=======+
| 0 | 2 | 1 |
+---+-------+-------+
| 1 | 5 | 2 |
+---+-------+-------+
| 2 | 8 | 3 |
+---+-------+-------+
| ... |
现在我想知道这是 MySQL(或通常的 SQL)的明确定义的行为还是不是(即结果取决于实现)?
For statements such as SELECT, you might get the results you expect, but this is not guaranteed. In the following statement, you might think that MySQL will evaluate @a first and then do an assignment second:
SELECT @a, @a:=@a+1, ...;
However, the order of evaluation for expressions involving user variables is undefined.
MySQL中如何定义临时变量操作?似乎值的任何变化都会在赋值表达式之后立即 "visible" 。如果我 运行 查询
SELECT
@x as x,
@x + (@x:=@x+1) + @x as magic,
@x as new_x
FROM
(SELECT @x:=0) x,
Values1to100 numbers
它returns
+---+-------+-------+
| x | magic | new_x |
+===+=======+=======+
| 0 | 2 | 1 |
+---+-------+-------+
| 1 | 5 | 2 |
+---+-------+-------+
| 2 | 8 | 3 |
+---+-------+-------+
| ... |
现在我想知道这是 MySQL(或通常的 SQL)的明确定义的行为还是不是(即结果取决于实现)?
For statements such as SELECT, you might get the results you expect, but this is not guaranteed. In the following statement, you might think that MySQL will evaluate @a first and then do an assignment second:
SELECT @a, @a:=@a+1, ...;
However, the order of evaluation for expressions involving user variables is undefined.