MySQL 語法在存在 null 的情況下使用不等於運算子時無法進行求值?
使用 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 −> ); Query OK, 0 rows affected (0.54 sec)
在表中插入一些帶有 null 值的記錄,以避免出現 null。插入記錄的查詢如下 −
mysql> insert into IsNullDemo values(100,'First-Product',null); Query OK, 1 row affected (0.14 sec) mysql> insert into IsNullDemo values(101,'Second-Product',2); Query OK, 1 row affected (0.22 sec) mysql> insert into IsNullDemo values(102,'Third-Product',null); Query OK, 1 row affected (0.20 sec) mysql> insert into IsNullDemo values(103,'Fourth-Product',4); Query OK, 1 row affected (0.17 sec) mysql> insert into IsNullDemo values(104,'Fifth-Product',10); Query OK, 1 row affected (0.17 sec) mysql> insert into IsNullDemo values(105,'Sixth-Product',null); Query OK, 1 row affected (0.20 sec)
使用 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)
情況 1
這是避免出現 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)
情況 2
只要你希望出現 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)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP