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

时间:2022-04-17 13:53:36

What is the difference in between

两者之间有什么区别

set test_var = 20;

and

set test_var:=20;

as they both seem to assign the value ?

因为他们似乎都分配了价值?

3 个解决方案

#1


9  

Both of them are assignment operators but one thing I can find their differences is that = can be used to perform boolean operation while := cannot.

它们都是赋值运算符,但有一点我可以发现它们的区别在于=可以用于执行布尔运算,而:=不能。

valid: SUM(val = 0)
Invalid: SUM(val := 0)

有效:SUM(val = 0)无效:SUM(val:= 0)

FROM User-Defined Variables

来自用户定义的变量

One more thing, You can also assign a value to a user variable in statements other than SET. In this case, the assignment operator must be := and not = because the latter is treated as the comparison operator = in non-SET statements.

还有一件事,您还可以在SET以外的语句中为用户变量赋值。在这种情况下,赋值运算符必须是:=而不是=因为后者在非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 | 
+------+------+------+--------------------+

#2


8  

It's more or less Syntactic sugar.

它或多或少是句法糖。

Take a look here

看看这里

Most important difference is

最重要的区别是

Unlike =, the := operator is never interpreted as a comparison operator. This means you can use := in any valid SQL statement (not just in SET statements) to assign a value to a variable.

与=不同,:=运算符永远不会被解释为比较运算符。这意味着您可以在任何有效的SQL语句中使用:=(而不仅仅是在SET语句中)为变量赋值。

#3


1  

You can only use := for assignment - never for comparison. It's just a bit of syntactic sugar, it doesn't really change the functionality at all. You'll see it a lot in generated SQL from code.

您只能使用:=进行分配 - 永远不会用于比较。它只是一些语法糖,它根本没有真正改变功能。你会从代码生成的SQL中看到很多东西。

#1


9  

Both of them are assignment operators but one thing I can find their differences is that = can be used to perform boolean operation while := cannot.

它们都是赋值运算符,但有一点我可以发现它们的区别在于=可以用于执行布尔运算,而:=不能。

valid: SUM(val = 0)
Invalid: SUM(val := 0)

有效:SUM(val = 0)无效:SUM(val:= 0)

FROM User-Defined Variables

来自用户定义的变量

One more thing, You can also assign a value to a user variable in statements other than SET. In this case, the assignment operator must be := and not = because the latter is treated as the comparison operator = in non-SET statements.

还有一件事,您还可以在SET以外的语句中为用户变量赋值。在这种情况下,赋值运算符必须是:=而不是=因为后者在非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 | 
+------+------+------+--------------------+

#2


8  

It's more or less Syntactic sugar.

它或多或少是句法糖。

Take a look here

看看这里

Most important difference is

最重要的区别是

Unlike =, the := operator is never interpreted as a comparison operator. This means you can use := in any valid SQL statement (not just in SET statements) to assign a value to a variable.

与=不同,:=运算符永远不会被解释为比较运算符。这意味着您可以在任何有效的SQL语句中使用:=(而不仅仅是在SET语句中)为变量赋值。

#3


1  

You can only use := for assignment - never for comparison. It's just a bit of syntactic sugar, it doesn't really change the functionality at all. You'll see it a lot in generated SQL from code.

您只能使用:=进行分配 - 永远不会用于比较。它只是一些语法糖,它根本没有真正改变功能。你会从代码生成的SQL中看到很多东西。