What is the purpose of the = operator in MySQL?

9

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

This returned the only two items you have in the database, both for <=> and = . Another test I did was with <=> , which returns all items that the = column is different <> , for this avatar up.

What is the purpose of the NULL operator in MySQL? Is there equality with the equality operator? When should it be used?

    
asked by anonymous 09.06.2017 / 21:46

2 answers

10

It is "equal" that deals with nulls without causing problems. So the null is treated as a normal value but different from all others. The normal behavior is that an operation involving a NULL value always results in NULL It changes this.

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

Documentation .

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

    
09.06.2017 / 21:53
2

The operator you used is the "Null safe equal". In the "=", if one of the two sides is NULL, it will return NULL, in this one, return 1 if both are NULL and 0 if one of them is NULL.

link

    
09.06.2017 / 21:56