2012-04-05 34 views
2

我在本地服務器上有一個簡單的表。MySQL:「NOT IN()」函數不允許在結果集中使用「NULL」值嗎?

mysql> desc table ; 
+-------+---------+------+-----+---------+-------+ 
| Field | Type | Null | Key | Default | Extra | 
+-------+---------+------+-----+---------+-------+ 
| id | int(10) | YES |  | NULL |  | 
| count | int(10) | YES |  | NULL |  | 
+-------+---------+------+-----+---------+-------+ 
2 rows in set (0.00 sec 

它只有三條記錄。

mysql> select * from uday ; 
+------+-------+ 
| id | count | 
+------+-------+ 
| 1 |  1 | 
| 2 |  2 | 
| 3 |  0 | 
| 4 | NULL | 
+------+-------+ 
4 rows in set (0.00 sec) 

現在,爲什麼我沒有看到下面的結果中的第四列..?

mysql> select * from uday where count NOT IN (0) ; 
mysql> select * from uday where count != 0 ; 
+------+-------+ 
| id | count | 
+------+-------+ 
| 1 |  1 | 
| 2 |  2 | 
+------+-------+ 
2 rows in set (0.00 sec) 

第4條記錄怎麼樣...?它在結果中不可見。 NULL不是0 RIGHT ...?

請忽略,如果它看起來很愚蠢,因爲我甚至沒有編碼部分的競爭力。

回答

3
col1 not in (1,2,null) 

是簡寫:

col1 <> 1 and col1 <> 2 and col1 <> null 

在SQL的three-valued logiccol1 <> null回報unknown。並且true and true and unknown也返回unknown。由於where只接受true,而不是unknown,因此將null的行從結果集中濾除。

+0

其清晰的Andomar。 thanq的快速回復。 -Uday – Uday 2012-04-05 11:16:07

相關問題