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.
It is the NULL-safe equal to operator
<=> Operator is used to compare NULL values with the fields. If normal =(equals) Operators return NULL if one of the comparison value is NULL. With <=> operator returns true or false. <=> Operator is same as IS NULL.
From the manual:-
Edit:-(Although very late to add one important side note mentioning NOT <=> as well)
On a side note:-
NOT <=>
There is one more point NOT <=> which is used to compare NULL values with the fields. If normal != or <> (not equals) Operators return NULL if one of the comparison value is NULL. With NOT applied to <=> operator returns true or false. NOT applied to <=> Operator is same as IS NOT NULL.
Example:-
From the MySQL documentation:
An example using the
<=>
operator would be:Which would return:
An example of the regular
=
operator would be:Which would return:
The
<=>
operator is very similar to the=
operator, except<=>
will never returnNULL
Similarity with
=
operatorLike the regular
=
operator, two values are compared and the result is either0
(not equal) or1
(equal); in other words:'a' <=> 'b'
yields0
and'a' <=> 'a'
yields1
.Difference with
=
operatorUnlike the regular
=
operator, values ofNULL
don't have a special meaning and so it never yieldsNULL
as a possible outcome; so:'a' <=> NULL
yields0
andNULL <=> NULL
yields1
.Contrary to
=
, whereby'a' = NULL
yieldsNULL
and evenNULL = NULL
yieldsNULL
; BTW, almost all operators and functions in MySQL work in this manner, because comparing againstNULL
is basically undefined.Usefulness
This is very useful for when both operands may contain
NULL
and you need a consistent comparison result between two columns.Another use-case is with prepared statements, for example:
Here, the placeholder can be either a scalar value or
NULL
without having to change anything about the query.Related operators
Besides
<=>
there are also two other operators that can be used to compare againstNULL
, namelyIS NULL
andIS NOT NULL
; they're part of the ANSI standard and therefore supported on other databases, unlike<=>
, which is MySQL-specific.You can think of them as specializations of MySQL's
<=>
:Based on this, your particular query (fragment) can be converted to the more portable:
<=>
is MySQL's null-safe "equal to" operator. From the manual: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.
It significance:
When you compare a NULL value with a non-NULL value, you'll get NULL. If you want to check if a value is null.
The Equality operator(<=>) which considers NULL as a normal value, so it returns 1 (not NULL) if both values are NULL and returns 0 (not NULL) if one of the values is NULL:
eg
is <=>
NULL-safe equal to operator
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.
See here for the documentation
Sample :
you should use IS NOT NULL. (The comparison operators = and <> both give UNKNOWN with NULL on either side of the expression.)
can also negate the null safe equality operator but this is not standard SQL.