What is the difference between := and = operators?

Viewed 4213

What is the difference between := and = operators in MySql?

And which place is it stable to use these two?

Is it the same or just an alternative?

2 Answers

Answer

In a SET statement, both := and = are assignment operators.

In a SELECT statement, := is an assignment operator and = is an equality operator.

Example

SET @a = 1, @b := 2;
SELECT @a, @b;   -- 1, 2
SELECT @a = @b;  -- 0 (false)

SELECT @a := @b; -- 2
SELECT @a, @b;   -- 2, 2
SELECT @a = @b;  -- 1 (true)
Related