Table 12.6 Assignment Operators赋值运算符
Assignment operator. 赋值运算符。Causes the user variable on the left hand side of the operator to take on the value to its right. 使运算符左侧的用户变量取其右侧的值。The value on the right hand side may be a literal value, another variable storing a value, or any legal expression that yields a scalar value, including the result of a query (provided that this value is a scalar value). 右侧的值可以是文字值、存储值的另一个变量,或者生成标量值的任何合法表达式,包括查询结果(前提是该值是标量值)。You can perform multiple assignments in the same 可以在同一SET
statement. SET
语句中执行多个赋值。You can perform multiple assignments in the same statement.可以在同一语句中执行多个赋值。
Unlike 与=
, the :=
operator is never interpreted as a comparison operator. =
不同之处在于::=
运算符从不被解释为比较运算符。This means you can use 这意味着您可以在任何有效的SQL语句(不仅仅在SET语句中)中使用:=
in any valid SQL statement (not just in SET
statements) to assign a value to a variable.:=
为变量赋值。
mysql>SELECT @var1, @var2;
-> NULL, NULL mysql>SELECT @var1 := 1, @var2;
-> 1, NULL mysql>SELECT @var1, @var2;
-> 1, NULL mysql>SELECT @var1, @var2 := @var1;
-> 1, 1 mysql>SELECT @var1, @var2;
-> 1, 1 mysql>SELECT @var1:=COUNT(*) FROM t1;
-> 4 mysql>SELECT @var1;
-> 4
You can make value assignments using 您可以在:=
in other statements besides SELECT
, such as UPDATE
, as shown here:SELECT
之外的其他语句中使用:=
进行值赋值,例如UPDATE
,如下所示:
mysql>SELECT @var1;
-> 4 mysql>SELECT * FROM t1;
-> 1, 3, 5, 7 mysql>UPDATE t1 SET c1 = 2 WHERE c1 = @var1:= 1;
Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql>SELECT @var1;
-> 1 mysql>SELECT * FROM t1;
-> 2, 3, 5, 7
While it is also possible both to set and to read the value of the same variable in a single SQL statement using the 虽然也可以使用:=
operator, this is not recommended. :=
运算符在单个SQL语句中设置和读取同一变量的值,但不建议这样做。Section 9.4, “User-Defined Variables”, explains why you should avoid doing this.第9.4节,“用户定义变量”解释了为什么应该避免这样做。
This operator is used to perform value assignments in two cases, described in the next two paragraphs.此运算符用于在两种情况下执行值赋值,将在下面两段中介绍。
Within a 在SET
statement, =
is treated as an assignment operator that causes the user variable on the left hand side of the operator to take on the value to its right. SET
语句中,=
被视为赋值运算符,使运算符左侧的用户变量取其右侧的值。(In other words, when used in a (换句话说,当在SET
statement, =
is treated identically to :=
.) SET
语句中使用时,=
被视为与:=
等同。)The value on the right hand side may be a literal value, another variable storing a value, or any legal expression that yields a scalar value, including the result of a query (provided that this value is a scalar value). 右侧的值可以是文字值、存储值的另一个变量,或者生成标量值的任何合法表达式,包括查询结果(前提是该值是标量值)。You can perform multiple assignments in the same 可以在同一SET
statement.SET
语句中执行多个赋值。
In the 在SET
clause of an UPDATE
statement, =
also acts as an assignment operator; in this case, however, it causes the column named on the left hand side of the operator to assume the value given to the right, provided any WHERE
conditions that are part of the UPDATE
are met. UPDATE
语句的SET
子句中,=
也充当赋值运算符;但是,在这种情况下,只要满足更新的任何条件,它就会使操作符左侧的列采用右侧给定的值。You can make multiple assignments in the same 可以在SET
clause of an UPDATE
statement.UPDATE
语句的同一SET
子句中进行多个赋值。
In any other context, 在任何其他上下文中,=
is treated as a comparison operator.=
被视为比较运算符。
mysql>SELECT @var1, @var2;
-> NULL, NULL mysql>SELECT @var1 := 1, @var2;
-> 1, NULL mysql>SELECT @var1, @var2;
-> 1, NULL mysql>SELECT @var1, @var2 := @var1;
-> 1, 1 mysql>SELECT @var1, @var2;
-> 1, 1
For more information, see Section 13.7.6.1, “SET Syntax for Variable Assignment”, Section 13.2.13, “UPDATE Statement”, and Section 13.2.11, “Subqueries”.有关更多信息,请参阅第13.7.6.1节,“变量赋值的设置语法”、第13.2.13节,“更新语句”和第13.2.11节,“子查询”。