您可以IF()
用来检查数据是否为NULL。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(200), Age int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable(Name,Age) values('John',23); mysql> insert into DemoTable(Name,Age) values('Sam',null); mysql> insert into DemoTable(Name,Age) values('Mike',23); mysql> insert into DemoTable(Name,Age) values('David',21); mysql> insert into DemoTable(Name,Age) values('Carol',null);
使用选择命令显示表中的记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | John | 23 | | 2 | Sam | NULL | | 3 | Mike | 23 | | 4 | David | 21 | | 5 | Carol | NULL | +----+-------+------+ 5 rows in set (0.00 sec)
这是检查数据是否为NULL的查询。只要记录中的NULL可见,就会添加一条消息-
mysql> select if(Age IS NULL,'Age is missing',Age) from DemoTable;
这将产生以下输出-
+--------------------------------------+ | if(Age IS NULL,'Age is missing',Age) | +--------------------------------------+ | 23 | | Age is missing | | 23 | | 21 | | Age is missing | +--------------------------------------+ 5 rows in set (0.00 sec)