In MariaDB' s client mycli
I am trying to SELECT
items whose value vend_country
is either NULL
or MI
so I wrote this simple query:
SELECT *
FROM vendors
WHERE vend_state IN (NULL,'MI');
And I got this output:
+-----------+-------------+-----------------+-------------+--------------+------------+----------------+
| vend_id | vend_name | vend_address | vend_city | vend_state | vend_zip | vend_country |
|-----------+-------------+-----------------+-------------+--------------+------------+----------------|
| 1001 | Anvils R Us | 123 Main Street | Southfield | MI | 48075 | USA |
+-----------+-------------+-----------------+-------------+--------------+------------+----------------+
Which is weird, because it seems to be missing the rows with NULL
value that are found in the same table. This query demonstrates this:
SELECT *
FROM vendors;
The output clearly shows two rows with null value which should be outputted with first query:
+-----------+----------------+-----------------+-------------+--------------+------------+----------------+
| vend_id | vend_name | vend_address | vend_city | vend_state | vend_zip | vend_country |
|-----------+----------------+-----------------+-------------+--------------+------------+----------------|
| 1001 | Anvils R Us | 123 Main Street | Southfield | MI | 48075 | USA |
| 1002 | LT Supplies | 500 Park Street | Anytown | OH | 44333 | USA |
| 1003 | ACME | 555 High Street | Los Angeles | CA | 90046 | USA |
| 1004 | Furball Inc. | 1000 5th Avenue | New York | NY | 11111 | USA |
| 1005 | Jet Set | 42 Galaxy Road | London | <null> | N16 6PS | England |
| 1006 | Jouets Et Ours | 1 Rue Amusement | Paris | <null> | 45678 | France |
+-----------+----------------+-----------------+-------------+--------------+------------+----------------+
I also tried some other queries which also ignore rows with NULL
value:
mariadb root@localhost:crashcourse> SELECT *
-> FROM vendors
-> WHERE vend_state IN ('<null>','MI');
+-----------+-------------+-----------------+-------------+--------------+------------+----------------+
| vend_id | vend_name | vend_address | vend_city | vend_state | vend_zip | vend_country |
|-----------+-------------+-----------------+-------------+--------------+------------+----------------|
| 1001 | Anvils R Us | 123 Main Street | Southfield | MI | 48075 | USA |
+-----------+-------------+-----------------+-------------+--------------+------------+----------------+
mariadb root@localhost:crashcourse> SELECT *
-> FROM vendors
-> WHERE vend_state IN ('NULL','MI');
+-----------+-------------+-----------------+-------------+--------------+------------+----------------+
| vend_id | vend_name | vend_address | vend_city | vend_state | vend_zip | vend_country |
|-----------+-------------+-----------------+-------------+--------------+------------+----------------|
| 1001 | Anvils R Us | 123 Main Street | Southfield | MI | 48075 | USA |
+-----------+-------------+-----------------+-------------+--------------+------------+----------------+
On the other hand this works completely fine, so IN
keyword must be working:
SELECT *
FROM vendors
WHERE vend_id IN (1001,1005);
+-----------+-------------+-----------------+-------------+--------------+------------+----------------+
| vend_id | vend_name | vend_address | vend_city | vend_state | vend_zip | vend_country |
|-----------+-------------+-----------------+-------------+--------------+------------+----------------|
| 1001 | Anvils R Us | 123 Main Street | Southfield | MI | 48075 | USA |
| 1005 | Jet Set | 42 Galaxy Road | London | <null> | N16 6PS | England |
+-----------+-------------+-----------------+-------------+--------------+------------+----------------+
So can anyone tell me, how can I search for NULL
value using IN
keyword?