Mega Code Archive

 
Categories / MySQL Tutorial / Select Query
 

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 = 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>