2012-04-10 24 views
3

我有2個表(TABLE1 & TABLE2)。我想執行以下查詢:SQL:如何強制關係運算符考慮NULL值

UPDATE TABLE1 a,TABLE1 b 
SET a.desg=CASE WHEN b.attribute_id=74 THEN b.value ELSE a.desc END 
WHERE a.entity_id=b.entity_id; 

但是我在TABLE1中有一些行,entity_id爲NULL。
評估時沒有考慮到這些WHERE a.entity_id=b.entity_id;

我甚至希望在這個WHERE子句中考慮NULL。

我該怎麼做?提前致謝。

+0

大概是兩個引用'TABLE1'是一個錯字? – APC 2012-04-10 11:51:27

+0

TABLE2中有多少條記錄(或者是別名爲'b')有一個null'entity_id'和'attribute_id = 74'?我希望它是一個! – APC 2012-04-10 11:53:31

回答

2

嘗試使用類似

WHERE IFNULL(a.entity_id, 0) = IFNULL(b.entity_id, 0); 
+1

另一種解決方案是'WHERE COALESCE(a.entity_id,0)= COALESCE(b.entity_id,0);'它返回NON NULL值的第一次出現。 – 2012-04-10 11:18:14

+0

這很簡單。謝謝Andrey .... --Uday – Uday 2012-04-10 11:18:32

+0

確保在使用此方法時沒有實際具有'entity_id = 0'的實體。 – 2012-04-10 11:34:54

5
UPDATE TABLE1 a, TABLE1 b 
SET a.desg = CASE WHEN b.attribute_id=74 THEN b.value ELSE a.desc END 
WHERE a.entity_id=b.entity_id OR (a.entity_id IS NULL AND b.entity_id IS NULL) 
+0

THanks levanlevi .... – Uday 2012-04-10 11:18:51