如何在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)
以下是使用插入命令将记录插入表中的查询:
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)
阅读更多:MySQL 教程
极客教程