2012-04-23 83 views
0

我有一個表,我必須檢查一個特定的列在空值或有價值。
可以說列名是order_price
查詢檢查是否存在記錄和列空

如果我只是檢查where order_price is null那麼這也包括不在表中的記錄。
例如我有order_id = 1其orders_price爲空,並且我有order_id = 2,這在訂單表中不存在。因此,而不是where條件來檢查order_price是否爲空,我想知道列是否爲空或記錄不存在。

我正在做這個表的外連接,所以我不能映射另一個表的主鍵。我正在使用Oracle。

感謝

+1

向我們顯示您的查詢。 – 2012-04-23 21:08:15

回答

3
SELECT o.*, 
     od.order_price, 
     CASE 
     WHEN od.order_id IS NULL THEN 
       'Not exists' 
     WHEN od.order_price IS NULL THEN 
       'Exists, IS NULL' 
     ELSE 
       'Exists, IS NOT NULL' 
     END AS nullness 
FROM orders o 
LEFT JOIN 
     order_data od 
ON  od.order_id = o.id 
2

取而代之的加入,則可以使用 「EXISTS/NOT EXISTS」 關鍵字和子查詢。

例如

SELECT parent.* 
FROM parent 
WHERE EXISTS (SELECT 1 
       FROM child 
       WHERE child.id_parent = parent.id AND child.somecolumn IS NULL) 

根據你想要還是不想讓謂詞匹配,你可以使用「exists/not exist」來玩。