Assignment operators
seekdb supports the commonly used assignment operators.
Overview of logical operators
| Operator | Operand | Meaning |
|---|---|---|
| := | Unary | Assigns the value on the right side of the operator to the variable on the left side. |
| = | Unary | Assignment (as part of a SET statement, or as part of the SET clause in an UPDATE statement) |
Description
Assignment operator :=
The value on the right side of the := operator can be a literal, a variable, or a valid expression, including the result of a query (provided that the value is a scalar value). You can perform multiple assignments in the same SET statement. You can also perform multiple assignments in the same statement.
Unlike =, the := operator is never resolved as a comparison operator. This means that you can use := to assign a value to a variable in any valid SQL statement, not just in a SET statement.
Here is an example:
SELECT @var1 := 1, @var2;
+------------+-------+
| @var1 := 1 | @var2 |
+------------+-------+
| 1 | NULL |
+------------+-------+
1 row in set (0.001 sec)
SELECT @var1:=COUNT(*) FROM t1;
+-----------------+
| @var1:=COUNT(*) |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.004 sec)
SELECT @var1;
+-------+
| @var1 |
+-------+
| 5 |
+-------+
1 row in set (0.001 sec)
Assignment operator =
The = operator performs assignment in the following two scenarios:
-
In a
SETstatement,=is considered an assignment operator that assigns the value on the right side of the operator to the variable on the left side. Therefore, when used in aSETstatement,=behaves the same way as:=. -
In the
SETclause of anUPDATEstatement,=also serves as an assignment operator. In this case, if theWHEREcondition in theUPDATEstatement is met, the value on the right side of the operator is assigned to the column on the left side. You can perform multiple assignments in the sameSETclause of anUPDATEstatement.
In all other scenarios, = is considered a comparison operator.
SELECT * FROM t1;
+------+------+
| c1 | c2 |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.003 sec)
UPDATE t1 SET c1 = c1 + 1, c2 = c1;
Query OK, 1 row affected (0.003 sec)
Rows matched: 1 Changed: 1 Warnings: 0
SELECT * FROM t1;
+------+------+
| c1 | c2 |
+------+------+
| 2 | 2 |
+------+------+
1 row in set (0.002 sec)