:=和=运算符有什么区别? [英] What is the difference between := and = operators?

查看:204
本文介绍了:=和=运算符有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySql中的:==运算符之间有什么区别?

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?

推荐答案

从另一个问题中我知道您的意思是

From your other question I know that you mean in the use case of

SELECT variable = column FROM table;

继续前进,亲眼看看...

Go ahead and see for yourself...

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等于false,结果1等于true.

In the result a 0 in the last column equals false, a 1 equals true.

SELECT @asdf := id FROM foo;

返回

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

因为id的值已分配给变量@asdf

because the value of id gets assigned to the variable @asdf

如果您现在发出一个

SELECT @asdf;

它返回

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

因为包含3的行是最后选择的.

because the row containing 3 was last selected.

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

返回

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

现在

SELECT @asdf;

返回

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

区别现在已经清楚了吗?

Difference is clear now?

这篇关于:=和=运算符有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆