Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 988
hsuk Avatar asked Mar 25 '13 11:03

hsuk


People also ask

What is difference between <= and := operators?

= operator assigns a value either as a part of the SET statement or as a part of the SET clause in an UPDATE statement, in any other case = operator is interpreted as a comparison operator. On the other hand, := operator assigns a value and it is never interpreted as a comparison operator.

Which one is faster between <> and != MySQL?

There is no difference. According to SQL.org, the !=

What is not equal in MySQL?

not equal to (<>, !=) operator. MySQL Not equal is used to return a set of rows (from a table) after making sure that two expressions placed on either side of the NOT EQUAL TO (<>) operator are not equal.


2 Answers

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)

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.

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 | 
+------+------+------+--------------------+
like image 98
John Woo Avatar answered Oct 11 '22 12:10

John Woo


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.

like image 40
DonCallisto Avatar answered Oct 11 '22 13:10

DonCallisto