使用IS NOT NULL运算符与NULL值进行比较。语法如下-
SELECT *FROM yourTableName where yourColumnName1 is not null or yourColumnName2 <> anyIntegerValue;
要检查不等于null的情况,让我们创建一个表。创建表的查询如下-
mysql> create table IsNullDemo −> ( −> ProductId int, −> ProductName varchar(100), −> ProductBackOrder int −> );
在表中插入一些具有空值的记录,以避免出现空值。插入记录的查询如下-
mysql> insert into IsNullDemo values(100,'First-Product',null); mysql> insert into IsNullDemo values(101,'Second-Product',2); mysql> insert into IsNullDemo values(102,'Third-Product',null); mysql> insert into IsNullDemo values(103,'Fourth-Product',4); mysql> insert into IsNullDemo values(104,'Fifth-Product',10); mysql> insert into IsNullDemo values(105,'Sixth-Product',null);
使用select语句显示表中的所有记录。显示所有记录的查询如下-
mysql> select *from IsNullDemo;
以下是输出-
+-----------+----------------+------------------+ | ProductId | ProductName | ProductBackOrder | +-----------+----------------+------------------+ | 100 | First-Product | NULL | | 101 | Second-Product | 2 | | 102 | Third-Product | NULL | | 103 | Fourth-Product | 4 | | 104 | Fifth-Product | 10 | | 105 | Sixth-Product | NULL | +-----------+----------------+------------------+ 6 rows in set (0.00 sec)
这是避免出现null的查询。查询如下-
mysql> select *from IsNullDemo −> where ProductBackOrder is not null or ProductBackOrder <> 2;
以下是输出-
+-----------+----------------+------------------+ | ProductId | ProductName | ProductBackOrder | +-----------+----------------+------------------+ | 101 | Second-Product | 2 | | 103 | Fourth-Product | 4 | | 104 | Fifth-Product | 10 | +-----------+----------------+------------------+ 3 rows in set (0.03 sec)
每当您希望存在null(或不等于2)时,请使用IS NULL概念。查询如下-
mysql> select *from IsNullDemo −> where ProductBackOrder is null or ProductBackOrder <> 2;
以下是输出-
+-----------+----------------+------------------+ | ProductId | ProductName | ProductBackOrder | +-----------+----------------+------------------+ | 100 | First-Product | NULL | | 102 | Third-Product | NULL | | 103 | Fourth-Product | 4 | | 104 | Fifth-Product | 10 | | 105 | Sixth-Product | NULL | +-----------+----------------+------------------+ 5 rows in set (0.00 sec)