ID EN
Assignment Operators

=

MySQL 8.4

This operator is used to perform value assignments in two cases, described in the next two paragraphs.

Syntax

MYSQL
SELECT @var1, @var2;
NULL, NULL
SELECT @var1 := 1, @var2;
1, NULL
SELECT @var1, @var2;
1, NULL
SELECT @var1, @var2 := @var1;
1, 1
SELECT @var1, @var2;
1, 1

Examples

Example

Within a SET statement, = is treated as an assignment operator that causes the user variable on the left hand side of the operator to take on the value to its right. (In other words, when used in a SET statement, = is treated identically to :=.) The value on the right hand side may be a literal value, another variable storing a value, or any legal expression that yields a scalar value, including the result of a query (provided that this value is a scalar value). You can perform multiple assignments in the same SET statement.

MYSQL
SELECT @var1, @var2;
NULL, NULL
SELECT @var1 := 1, @var2;
1, NULL
SELECT @var1, @var2;
1, NULL
SELECT @var1, @var2 := @var1;
1, 1
SELECT @var1, @var2;
1, 1