2013-04-26 50 views
0

嘿,你好,你需要你的幫助,我很多嘗試,現在我累了找不到出路。如何刪除表1中不存在的行

我有兩個表&我沒有內部聯接要刪除的那些行,外國ID不存在於表2,下面我已經通過結構提到..

表1

 Column A(Foreign)  Column B 

    record A    Some thing 
    record B    Some thing 
    record c    Some thing 

表2

 Column A(Foreign)  Column B 

    record A    Some thing 
    record B    Some thing 

現在其實我想刪除不在表2中的記錄C ..是否有任何出路?

+0

請後,即使它沒有產生正確的結果你最大的努力。至少我們會知道我們**不需要解釋。 – 2013-04-26 04:10:14

回答

1
DELETE FROM `Table 1` t1 
WHERE NOT EXISTS (
    SELECT 1 FROM `Table 2` t2 
    WHERE t2.`Column A(Foreign)` = t1.`Column A(Foreign)` 
) 

可怕的表和列名,順便

演示在這裏 - http://sqlfiddle.com/#!2/4c2d8/1

0

這工作:

DELETE VM.* FROM `Table1` AS VM 
LEFT JOIN `Table2` AS VL 
ON VL.`Column A(Foreign)` = VM.`Column A(Foreign)d` 
WHERE VL.`Column A(Foreign)` IS NULL 
-1

試試這個,工作。

http://sqlfiddle.com/#!2/d5f02/1

create table t1 (
    a varchar(16), 
    b varchar(16) 
); 

create table t2 (
    a varchar(16), 
    b varchar(16) 
); 

insert into t1 values 
('record A', 'Some thing'), 
('record B', 'Some thing'), 
('record c', 'Some thing'); 

insert into t2 values 
('record A', 'Some thing'), 
('record B', 'Some thing'); 

delete t1 
FROM t1 left outer join t2 
ON t1.a = t2.a 
where t2.a is null 
+0

你確定你得到OP想要的結果嗎? – 2013-04-26 04:41:20

+0

@ PM77-1感謝您指出,請現在檢查我已更正了代碼。 – 2013-04-26 05:39:38