0

嘿伙计们嗨需要你的帮助我尝试了很多,现在我累了找不到出路。

我有两个表,我做了内部连接,想删除表 2 中不存在外部 id 的那些行,下面我按结构提到了..

表格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 ..有什么办法吗???

4

3 回答 3

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

于 2013-04-26T04:12:32.107 回答
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
于 2013-04-26T04:16:55.513 回答
-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
于 2013-04-26T04:24:49.917 回答