MySQL中 =
和 :=
的区别
=
只有在 set 和 update 时才是和 :=
一样,是赋值的作用,其它都是 等于 的作用。
mysql> Set @num = 3; -- 对 @num 赋值
Query OK, 0 rows affected (0.00 sec)
mysql> Select @num = 1; -- 此时 = 不是赋值,而是等于。 即 3 = 1,为假,返回 0。
+----------+
| @num = 1 |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql> Select @num; -- 得到 @num的值,并且再次验证 Select @num = 1; 不是赋值
+------+
| @num |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
mysql> Select @num = 3; -- @num = 3,为真,返回 1。
+----------+
| @num = 3 |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
:=
不只在 set 和 update 时是赋值的作用,在 select 也是赋值的作用。
mysql> Set @num = 3;
Query OK, 0 rows affected (0.00 sec)
mysql> Select @num := 1;
+-----------+
| @num := 1 |
+-----------+
| 1 |
+-----------+
1 row in set, 1 warning (0.00 sec)
mysql> Select @num;
+------+
| @num |
+------+
| 1 |
+------+
1 row in set (0.00 sec)