什么是“< =>”在MySQL意味着什么?

时间:2010-12-29 12:07:08

标签: mysql null operators equality

MySQL中的<=>意味着什么?

5 个答案:

答案 0 :(得分:15)

manual说明了一切:

  

NULL-safe相等。这个运营商   执行相等的比较   =运算符,但返回1   如果两个操作数都为NULL,则为NULL,   如果是一个操作数,则为0而不是NULL   是NULL。

mysql> select NULL <=> NULL;
+---------------+
| NULL <=> NULL |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

mysql> select NULL = NULL;
+-------------+
| NULL = NULL |
+-------------+
|        NULL |
+-------------+
1 row in set (0.00 sec)

mysql> select NULL <=> 1;
+------------+
| NULL <=> 1 |
+------------+
|          0 |
+------------+
1 row in set (0.00 sec)

mysql> select NULL = 1;
+----------+
| NULL = 1 |
+----------+
|     NULL |
+----------+
1 row in set (0.00 sec)

mysql> 

答案 1 :(得分:4)

这是NULL-safe equal operator

&lt; =&gt;之间的差异和=是当一个或两个操作数都是NULL值时。例如:

NULL <=> NULL gives True
NULL = NULL   gives NULL

以下是值{1,2}和NULL的<=>比较的完整表:

     |  1      2    NULL
-----+-------------------
1    | True   False False
2    | False  True  False
NULL | False  False True

与普通的相等运算符比较:

     |  1      2    NULL
-----+-------------------
1    | True   False NULL
2    | False  True  NULL
NULL | NULL   NULL  NULL

答案 2 :(得分:2)

答案 3 :(得分:2)

<=>是所谓的NULL-safe-equality operator

SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL; 
-> 1, 1, 0

SELECT 1 = 1, NULL = NULL, 1 = NULL;
-> 1, NULL, NULL

答案 4 :(得分:1)

它与SQL标准关键字DISTINCT

相同
SELECT * FROM somewhere WHERE `address1` is not distinct from `address2`