要在MySQL中跳过空白和null,请使用以下语法:
select *from yourTableName where yourColumnName IS NOT NULL AND yourColumnName <> '';
让我们首先创建一个表:
mysql> create table DemoTable (Id int, FirstName varchar(20));
以下是使用insert命令在表中插入记录的查询:
mysql> insert into DemoTable values(100,'Larry'); mysql> insert into DemoTable values(101,''); mysql> insert into DemoTable values(102,'Chris'); mysql> insert into DemoTable values(103,null); mysql> insert into DemoTable values(104,' '); mysql> insert into DemoTable values(105,'Robert'); mysql> insert into DemoTable values(106,null);
以下是使用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)