如何跳过 MySQL 中的空值和 null 值?
若要在 MySQL 中跳过空值和 null 值,请使用以下语法
select *from yourTableName where yourColumnName IS NOT NULL AND yourColumnName <> '';
我们首先创建一个表
mysql> create table DemoTable (Id int, FirstName varchar(20)); Query OK, 0 rows affected (0.66 sec)
以下是使用 insert 命令在表中插入记录的查询
mysql> insert into DemoTable values(100,'Larry'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(101,''); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(102,'Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(103,null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(104,' '); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(105,'Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(106,null); Query OK, 1 row affected (0.13 sec)
以下是使用 select 命令从表中显示记录的查询
mysql> select *from DemoTable;
这将生成以下带有空值和 NULL 的输出
+------+-----------+ | Id | FirstName | +------+-----------+ | 100 | Larry | | 101 | | | 102 | Chris | | 103 | NULL | | 104 | | | 105 | Robert | | 106 | NULL | +------+-----------+ 7 rows in set (0.00 sec)
以下是跳过空值以及 MySQL 中 null 的查询
mysql> select *from DemoTable where FirstName IS NOT NULL AND FirstName <> '';
这将生成以下输出
+------+-----------+ | Id | FirstName | +------+-----------+ | 100 | Larry | | 102 | Chris | | 105 | Robert | +------+-----------+ 3 rows in set (0.03 sec)
广告