What does <=>
in MySQL mean and do?
Asked
Active
Viewed 4,764 times
17
-
possible duplicate of [What is this operator <=> in MySQL?](http://stackoverflow.com/questions/21927117/what-is-this-operator-in-mysql) – Salman A Jan 23 '15 at 14:20
-
This one is 3 years older... – kuba Feb 02 '15 at 08:09
-
that one has 6x more views and 2x answers. SO is a strange place. – Salman A Feb 02 '15 at 09:05
5 Answers
16
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>

codaddict
- 445,704
- 82
- 492
- 529
4
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

Mark Byers
- 811,555
- 193
- 1,581
- 1,452
2
NULL-safe equal to operator
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_equal-to
2
<=>
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

Stefan Gehrig
- 82,642
- 24
- 155
- 189
1
It's the same as SQL standard keyword DISTINCT
SELECT * FROM somewhere WHERE `address1` is not distinct from `address2`

Milan Babuškov
- 59,775
- 49
- 126
- 179