What does the MySQL <=> statement do?

What is MySQL <=>?

Since the operator is a symbol, it is difficult to find documentation. (Like a ternary operator ?: For programming languages ​​that support them.)

I got it from an example in a book.

 mysql> select null <=> null; +---------------+ | null <=> null | +---------------+ | 1 | +---------------+ 1 row in set (0.00 sec) 
+4
source share
3 answers

This is an operator with zero safe comparison. And this is awesome.

This means that if you are trying to query your database for a variable, such as a string, which can sometimes be null, you want to use it. For example, if you try to search for SELECT * FROM table WHERE x = NULL , it will not return anything, but if you do SELECT * FROM table WHERE x <=> NULL , this will work.

+6
source

NULL is safe. This operator does an equality comparison, like the = operator, but returns 1, not NULL if both operands are NULL, but 0, not NULL if one operand is NULL.

  • mysql> SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL;
  • β†’ 1, 1, 0
  • mysql> SELECT 1 = 1, NULL = NULL, 1 = NULL;
  • β†’ 1, NULL, NULL

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

+3
source

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

NULL is safe. This operator does an equality comparison, like the = operator, but returns 1, not NULL if both operands are NULL, but 0, not NULL if one operand is NULL.

0
source

All Articles