2011-08-02 56 views
3

這個查詢提供了只要有超過1個記錄存在於TBL1,而不是在tbl1_temp更新WHERE NOT EXISTS問題

我怎樣才能解決這個錯誤,這樣對於不tbl1_temp存在的每一條記錄,更新相應的記錄在TBL1

UPDATE tbl1 SET Status = 'Finished' 
WHERE id = (SELECT id 
FROM tbl1 
WHERE NOT EXISTS (SELECT id FROM tbl1_temp 
WHERE tbl1.id = tbl1_temp.id)) 

回答

8
UPDATE tbl1 
SET Status = 'Finished' 
WHERE NOT EXISTS (SELECT id FROM tbl1_temp 
    WHERE tbl1.id = tbl1_temp.id) 
2

只需使用NOT狀態直接EXISTS

UPDATE tbl1 
SET Status = 'Finished' 
WHERE NOT EXISTS (SELECT * FROM tbl1_temp 
       WHERE tbl1.id = tbl1_temp.id) 
1

相等運算符僅適用於單個值,不適用於多個值,因此是錯誤。因此,而不是公式,使用IN

... WHERE id IN (SELECT ...) 
1
UPDATE tbl1 SET Status = 'Finished' 
WHERE id **IN** (SELECT id 
FROM tbl1 
WHERE NOT EXISTS (SELECT id FROM tbl1_temp 
WHERE tbl1.id = tbl1_temp.id))