2013-12-11 61 views

回答

6

從您的其他問題,我知道你在用例的

SELECT variable = column FROM table; 

來吧,看看自己的意思是......

CREATE TABLE foo (id int); 
INSERT INTO foo VALUES (1), (2), (3); 

SET @asdf = 2; 
SET @asdf := 2; /*those are the same*/ 
/*As SET is always an assignment operation, it doesn't matter here if you write it with := or with =*/ 
SELECT id, @asdf, @asdf = id FROM foo; 

回報

+------+-------+------------+ 
| id | @asdf | @asdf = id | 
+------+-------+------------+ 
| 1 |  2 |   0 | 
| 2 |  2 |   1 | 
| 3 |  2 |   0 | 
+------+-------+------------+ 

在最後一列中0的結果等於false1等於true

SELECT @asdf := id FROM foo; 

回報

+-------------+ 
| @asdf := id | 
+-------------+ 
|   1 | 
|   2 | 
|   3 | 
+-------------+ 

因爲id值被分配給變量@asdf

如果你現在發出

SELECT @asdf; 

返回

+-------+ 
| @asdf | 
+-------+ 
|  3 | 
+-------+ 

因爲最後選擇了包含3的行。

SELECT @asdf := id FROM foo ORDER BY id DESC; 

回報

+-------------+ 
| @asdf := id | 
+-------------+ 
|   3 | 
|   2 | 
|   1 | 
+-------------+ 

現在

SELECT @asdf; 

回報

+-------+ 
| @asdf | 
+-------+ 
|  1 | 
+-------+ 

區別是現在清楚了嗎?

相關問題