Search code examples
mysqldatabaseoperatorscolon-equals

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 ?


Solution

  • 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 | 
    +------+------+------+--------------------+