gpt4 book ai didi

mysql - SELECT 中的临时变量操作可见性

转载 作者:行者123 更新时间:2023-11-29 05:55:55 25 4
gpt4 key购买 nike

MySQL 中如何定义临时变量操作?似乎值的任何变化都会在赋值表达式之后立即“可见”。如果我运行查询

SELECT 
@x as x,
@x + (@x:=@x+1) + @x as magic,
@x as new_x
FROM
(SELECT @x:=0) x,
Values1to100 numbers

返回

+---+-------+-------+
| x | magic | new_x |
+===+=======+=======+
| 0 | 2 | 1 |
+---+-------+-------+
| 1 | 5 | 2 |
+---+-------+-------+
| 2 | 8 | 3 |
+---+-------+-------+
| ... |

现在我想知道这是 MySQL(或一般的 SQL)的明确定义的行为还是不是(即结果取决于实现)?

最佳答案

MySQL documentation

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 - SELECT 中的临时变量操作可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49434428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com