NULL値をチェック

IS NULL / IS NOT NULL演算子は、指定されたカラムがNULL値を持つかどうかを確認します。

IS NULL / IS NOT NULL
mysql> SELECT * FROM users;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | user1 |   10 |
|  2 | user2 | NULL |
|  3 | user3 |   30 |
|  4 | user4 | NULL |
|  5 | user5 |   50 |
+----+-------+------+
5 rows in set (0.00 sec)

【年齢がNULLなもの】
mysql> SELECT * FROM users where age IS NULL;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  2 | user2 | NULL |
|  4 | user4 | NULL |
+----+-------+------+
2 rows in set (0.00 sec)

【年齢がNULLではないもの】
mysql> SELECT * FROM users where age IS NOT NULL;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | user1 |   10 |
|  3 | user3 |   30 |
|  5 | user5 |   50 |
+----+-------+------+
3 rows in set (0.00 sec)