简体   繁体   中英

What does “<=>” in MySQL mean?

MySQL中的<=>是什么意思和做什么?

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> 

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

<=> 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

它与SQL标准关键字DISTINCT相同

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM