What is this operator <=> in MySQL?

2020-01-25 04:17发布

I'm working on code written by a previous developer and in a query it says,

WHERE p.name <=> NULL

What does <=> mean in this query? Is it something equal to =? Or is it a syntax error?

But it is not showing any errors or exceptions. I already know that <> = != in MySQL.

9条回答
神经病院院长
2楼-- · 2020-01-25 04:41

<=> is the NULL-safe equal operator. a <=> b is same as writing:

CASE
    WHEN a IS NULL AND b IS NULL THEN 1 -- both operands null then 1
    WHEN a IS NULL OR  b IS NULL THEN 0 -- one operand is null then 0
    ELSE a = b                          -- else behave like normal = operator
END

And sorry, I could not find one good reason to use this operator instead of AND/OR IS (NOT) NULL. Your example for example, WHERE p.name <=> NULL is same as WHERE p.name IS NULL.

查看更多
戒情不戒烟
3楼-- · 2020-01-25 04:41
mysql> SELECT * FROM t JOIN t2 WHERE t2.ids = t.ids;
+----+------+----+------+
| id | ids  | id | ids  |
+----+------+----+------+
|  1 |    1 |  1 |    1 |
|  2 |    2 |  2 |    2 |
|  5 |    6 |  5 |    6 |
|  6 |    7 |  6 |    7 |
+----+------+----+------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM t JOIN t2 WHERE t2.ids <=> t.ids;
+----+------+----+------+
| id | ids  | id | ids  |
+----+------+----+------+
|  1 |    1 |  1 |    1 |
|  2 |    2 |  2 |    2 |
|  3 | NULL |  3 | NULL |
|  4 | NULL |  3 | NULL |
|  3 | NULL |  4 | NULL |
|  4 | NULL |  4 | NULL |
|  5 |    6 |  5 |    6 |
|  6 |    7 |  6 |    7 |
查看更多
该账号已被封号
4楼-- · 2020-01-25 04:48

It is the NULL - Safe Equal to operator. Check description.

查看更多
登录 后发表回答