1
I have a table where I record purchases made by the site.
I use a field called status_plataforma
to receive the status of the Paypal transaction. This field, besides being able to receive an integer, by default it is NULL
.
In a certain query, I needed to know if the field status_plataforma
is not with the value 6
or 7
. For that, I’m using the condition WHERE status_plataforma NOT IN(6, 7)
.
However, when this field is at the value NULL
, the result is not found!
I mean, I don’t have 6
nor 7
in that column, but only NULL
and, theoretically, the result should be returned.
My Trials:
Example without NOT IN
:
mysql> select id, preco, status_plataforma, concluida FROM compras WHERE site_cadastro_aluno_id = 43;
+----+-------+-------------------+-----------+
| id | preco | status_plataforma | concluida |
+----+-------+-------------------+-----------+
| 41 | 29.90 | NULL | 0 |
+----+-------+-------------------+-----------+
1 row in set (0,00 sec)
The problem happens when I use NOT IN
:
mysql> select id, preco, status_plataforma, concluida FROM compras WHERE site_cadastro_aluno_id = 43 AND status_plataforma NOT IN (6,7);
Empty set (0,00 sec)
As seen in the previous consultation, in fact status_plataforma
is not even 6
nor 7
, but yes NULL
.
Why the NOT IN
had this behavior?
Related: Why NULL values are not selected?
– Marconi
Possible duplicate of Why NULL values are not selected?
– rbz
I don’t know who scored as duplicate, but I don’t see any connection with the problem of
NOT IN
– Wallace Maxters
@Wallacemaxters was me. It’s the same problem. There’s not even a way to make a new answer, it would be a copy.
– rbz
AND (status_plataforma NOT IN (6,7) OR status_plataforma IS NULL)
– rbz
SQL Server and Mysql would have the same behavior?
– Wallace Maxters
@Wallacemaxters!
– rbz
I did on top of the Sqlfiddle in SQL Server maniero: Sqlfiddle in Mysql
– rbz