2017-10-20 69 views
1

我在AWS一個蜂房表名爲table1僅帶有一列,它看起來像圖所示:蜂房在自動過濾NULL NOT IN條件

COL1 
(null) 
active 

試圖下面查詢

select * 
from table1 
where COL1 NOT IN ('reversed') 

蜂房是返回只有一行

COL1 
active 

爲什麼它不返回記錄(null)?

我知道修正是修改條件如下。但我想調試這個問題,以便這不會對其他查詢

where coalesce(COL1,"") NOT IN ('reversed') 

回答

1

這是所有RDBMS系統如何對待null值發生。

null有着特殊的意義 - 像not defined

當您檢查:

COL1 NOT IN ('reversed') 

以下比較將進行:

(null) != reversed 
active != reversed 
... 

,只有那些返回true會返回:

scala> spark.sql("SELECT 'active' != 'reversed'").show 
+-------------------------+ 
|(NOT (active = reversed))| 
+-------------------------+ 
|      true| 
+-------------------------+ 


scala> spark.sql("SELECT null != 'reversed'").show 
+---------------------------------------+ 
|(NOT (CAST(NULL AS STRING) = reversed))| 
+---------------------------------------+ 
|         null| 
+---------------------------------------+ 

,你可以看到:(null) != 'reversed'不返回true - 這就是爲什麼你沒有看到它在結果集中

此外:

scala> spark.sql("SELECT (null) = 'reversed'").show 
+---------------------------------+ 
|(CAST(NULL AS STRING) = reversed)| 
+---------------------------------+ 
|        null| 
+---------------------------------+ 

正因爲如此,我們有IS NULLIS NOT NULLCOALESCE等方法和功能,允許我們使用NULL

+0

謝謝澄清!它有幫助。 – Neena