What is the purpose of the <=> operator in Mysql?

Asked

Viewed 736 times

12

Still doing a series of tests in Mysql I noticed that the operator <=> worked equivalent to = for the case tested, the following:

SELECT * FROM `client` WHERE avatar <=> NULL

This returned the only two items it has in the base, both for <=> how much to =. Another test I did was with the <>, in which returns all the items that the column avatar is different NULL, for this select up.

What purpose of the operator <=> in Mysql? In fact there is equivalence with the equal operator? When should it be used?

2 answers

11


It is the "equal" that deals with the null without causing problems. Then the null is treated as a value normal but different from all others. Normal behavior is that an operation that involves a value NULL always results in NULL He changes it.

mysql> SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL;
        -> 1, 1, 0
mysql> SELECT 1 = 1, NULL = NULL, 1 = NULL;
        -> 1, NULL, NULL

I put in the Github for future reference.

Documentation.

In your example will give true or false according to the value of avatar that is expected not to be null. Using the simple equality operator would always result in NULL.

3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.