2013-10-31 76 views
0

我有A,B,C三個不同的表,我需要刪除一個表中不存在的B和C表中的記錄。如何從表中刪除兩個不同表中的不相等記錄

樣品:

delete from A 
where name NOT IN (select name from B inner join C); 
+6

步驟1.寫一個'SELECT'找出結果設置要刪除。第2步。將你的'SELECT'改爲'DELETE'。 – Kermit

+2

你的意思是'從不在的名字中刪除(從c中選擇名字的聯合名稱)'? – Joe

+0

@Joe可以給你一些其他的方式 – nag

回答

0

我喜歡用左聯接像這樣的東西。我開始通過創建一個select語句是這樣的:

SELECT * 
FROM TableA AS a 
     LEFT OUTER JOIN TableB AS b ON a.Name = b.Name 
     LEFT OUTER JOIN TableC AS c ON a.Name = c.Name 
WHERE b.Name IS NULL 
     AND c.NAME IS NULL 

然後我轉換爲DELETE語句是這樣的:

DELETE a 
FROM TableA AS a 
     LEFT OUTER JOIN TableB AS b ON a.Name = b.Name 
     LEFT OUTER JOIN TableC AS c ON a.Name = c.Name 
WHERE b.Name IS NULL 
     AND c.NAME IS NULL 
相關問題