MySQL选择查询以获取具有空值的数据?
让我们首先创建一个表-
mysql> create table DemoTable ( CustomerName varchar(100), CustomerCountryName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris','US'); mysql> insert into DemoTable values('Bob','UK'); mysql> insert into DemoTable values('Carol',NULL); mysql> insert into DemoTable values('David','AUS'); mysql> insert into DemoTable values('Mike',NULL);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------------+---------------------+ | CustomerName | CustomerCountryName | +--------------+---------------------+ | Chris | US | | Bob | UK | | Carol | NULL | | David | AUS | | Mike | NULL | +--------------+---------------------+ 5 rows in set (0.00 sec)
以下是获取具有空值的数据的查询-
mysql> select *from DemoTable where CustomerCountryName IS NULL;
这将产生以下输出-
+--------------+---------------------+ | CustomerName | CustomerCountryName | +--------------+---------------------+ | Carol | NULL | | Mike | NULL | +--------------+---------------------+ 2 rows in set (0.00 sec)