在MySQL中为空值和空值的单列实现两个条件
让我们首先创建一个表-
mysql> create table DemoTable635( EmployeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,EmployeeName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable635(EmployeeName) values('John'); mysql> insert into DemoTable635(EmployeeName) values('Sam'); mysql> insert into DemoTable635(EmployeeName) values(''); mysql> insert into DemoTable635(EmployeeName) values(null); mysql> insert into DemoTable635(EmployeeName) values('David');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable635;
这将产生以下输出-
+-----------+--------------+ | EmployeId | EmployeeName | +-----------+--------------+ | 1 | John | | 2 | Sam | | 3 | | | 4 | NULL | | 5 | David | +-----------+--------------+ 5 rows in set (0.00 sec)
以下是查询以避免显示MySQL列中的空值和空值-
mysql> select *from DemoTable635 tbl where tbl.EmployeeName is not null and tbl.EmployeeName <> '';
这将产生以下输出-
+-----------+--------------+ | EmployeId | EmployeeName | +-----------+--------------+ | 1 | John | | 2 | Sam | | 5 | David | +-----------+--------------+ 3 rows in set (0.02 sec)