开发者

What does MySQL <=> operator do?

开发者 https://www.devze.com 2022-12-11 05:02 出处:网络
What is the MySQL <=>? Because the operator is a symbol it is hard to look for documentation.(Similar to the ternary operator ?: for programing languages that suppor开发者_如何学JAVAt them.)

What is the MySQL <=>?

Because the operator is a symbol it is hard to look for documentation. (Similar to the ternary operator ?: for programing languages that suppor开发者_如何学JAVAt them.)

I got it from an example in a book.

mysql> select null <=> null;
+---------------+
| null <=> null |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)


It's a null-safe comparison operator. And it's awesome.

What that means is if you're trying to query your database for some variable, like a string, that might sometimes be null, you want to use it. For example, if you try searching SELECT * FROM table WHERE x = NULL it will return nothing, but if you do SELECT * FROM table WHERE x <=> NULL it'll work.


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


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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消