MySQL:选择列为空的行?
mysqlmysqli database
要选择列为空的行,您可以在 MySQL 中借助 where 子句使用 IS NULL。
语法如下 −
select *from yourTableName where yourColumnName IS NULL;
Let us first create a table to understand the concept −
mysql> create table NULLDemo1 -> ( -> StudentId int, -> StudentName varchar(100) -> ); Query OK, 0 rows affected (1.48 sec)
向表中插入记录。插入记录的查询如下 −
mysql> insert into NULLDemo1 values(NULL,'John'); Query OK, 1 row affected (0.25 sec) mysql> insert into NULLDemo1 values(100,'Johnson'); Query OK, 1 row affected (0.38 sec) mysql> insert into NULLDemo1 values(NULL,'Carol'); Query OK, 1 row affected (0.25 sec) mysql> insert into NULLDemo1 values(101,'Sam'); Query OK, 1 row affected (0.25 sec)
现在您可以借助 select 语句显示所有记录。查询如下 −
mysql> select *from NULLDemo1;
以下是输出 −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | NULL | John | | 100 | Johnson | | NULL | Carol | | 101 | Sam | +-----------+-------------+ 4 rows in set (0.00 sec)
应用上面开头讨论的语法来选择列为 NULL 的行。针对上表的查询如下。
mysql> select *from NULLDemo1 where StudentId IS NULL;
这是输出 −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | NULL | John | | NULL | Carol | +-----------+-------------+ 2 rows in set (0.00 sec)