What does “<=>” in MySQL mean?

蹲街弑〆低调 提交于 2019-12-06 17:11:21

问题


What does <=> in MySQL mean and do?


回答1:


The manual says it all:

NULL-safe equal. This operator performs an equality comparison like the = operator, but returns 1 rather than NULL if both operands are NULL, and 0 rather than NULL if one operand is 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> 



回答2:


It's the NULL-safe equal operator.

The difference between <=> and = is when one or both of the operands are NULL values. For example:

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

Here is the full table for the <=> comparison of values 1, 2 and NULL:

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

Compare to the ordinary equality operator:

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



回答3:


<=> is a so called 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:


NULL-safe equal to operator

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_equal-to




回答5:


It's the same as SQL standard keyword DISTINCT

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


来源:https://stackoverflow.com/questions/4553930/what-does-in-mysql-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!