[英]What is the difference between = and := in MySQL?
What is the difference in between两者有什么区别
set test_var = 20;
and和
set test_var:=20;
as they both seem to assign the value ?因为他们似乎都在分配价值?
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) 有效 :SUM(val = 0)
Invalid: 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. 还有一件事, 您还可以在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语句中被视为比较运算符=。
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 |
+------+------+------+--------------------+
It's more or less Syntactic sugar. 它或多或少是句法糖。
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语句中)中使用:=为变量赋值。
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中看到很多东西。
Using :=
in a SET
and SELECT
statement is interpreted as assignment.在
SET
和SELECT
语句中使用:=
被解释为赋值。
Using =
in a SET
statement is interpreted as assignment.在
SET
语句中使用=
被解释为赋值。
However, Using =
in a SELECT
statement is interpreted as a bool operator (comparison).但是,在
SELECT
语句中使用=
被解释为布尔运算符(比较)。
So if you mean assignment, it is always safe to use :=
因此,如果您的意思是分配,那么使用
:=
总是安全的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.