简体   繁体   English

MySQL中的=和=有什么区别?

[英]What's the difference between := and = in MySQL?

mysql> set @num := 1;
Query OK, 0 rows affected (0.00 sec)

mysql> set @num = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @num;
+------+
| @num |
+------+
|    0 |
+------+
1 row in set (0.00 sec)

Seems both works. 似乎两个都有效。

In short, when using SET they both act as assignment operators, but in any non-set statements, := is for assingment and = checks for equality . 简而言之,当使用SET它们都充当赋值运算符,但在任何非set语句中, :=表示assingment, =检查是否相等

For SET, either = or := can be used as the assignment operator. 对于SET,可以使用=或:=作为赋值运算符。

You can also assign a value to a user variable in statements other than SET. 您还可以在SET以外的语句中为用户变量赋值。 In this case, the assignment operator must be := and not = because = is treated as a comparison operator in non-SET statements 在这种情况下,赋值运算符必须是:=而不是=因为=在非SET语句中被视为比较运算符

mysql> SET @t1=1, @t2=2, @t3:=4;
mysql> SELECT @t1, @t2, @t3, @t4 := @t1+@t2+@t3;
+------+------+------+--------------------+
| @t1  | @t2  | @t3  | @t4 := @t1+@t2+@t3 |
+------+------+------+--------------------+
|    1 |    2 |    4 |                  7 | 
+------+------+------+--------------------+

Taken from MySQL 8.4 User Defined Variables 取自MySQL 8.4用户定义的变量

Both are assignment operator. 两者都是赋值运算符。

:= was created to be clearer since = is also an equivalence operator in SQL. :=创建为更清晰,因为=也是SQL中的等价运算符。 WHERE x = 1; (most computer languages it is == ) (大多数计算机语言是==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM