使用空值来获取数据的 MySQL 选择查询?
我们首先创建一个表-
mysql> create table DemoTable ( CustomerName varchar(100), CustomerCountryName varchar(100) ); Query OK, 0 rows affected (0.95 sec)
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris','US'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Bob','UK'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Carol',NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David','AUS'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Mike',NULL); Query OK, 1 row affected (0.18 sec)
使用选择语句从表中显示所有记录-
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)
广告