You can also assign a value to a user variable in statements other than SET : User Defined Variables « Select Query « MySQL Tutorial






In this case, the assignment operator must be := and not = because = is treated as a comparison operator in non-SET statements:

mysql>
mysql> SET @t1=0, @t2=1, @t3=2;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> SELECT @t1:=(@t2:=1)+@t3:=4,@t1,@t2,@t3;
+----------------------+------+------+------+
| @t1:=(@t2:=1)+@t3:=4 | @t1  | @t2  | @t3  |
+----------------------+------+------+------+
|                    5 | 5    | 1    | 4    |
+----------------------+------+------+------+
1 row in set (0.00 sec)

mysql>








2.22.User Defined Variables
2.22.1.User variables are written as @var_name.
2.22.2.You can also assign a value to a user variable in statements other than SET
2.22.3.Using User-Defined Variables
2.22.4.Use user-defined variable in the where statement
2.22.5.Create dynamic sql command