Mysql 如何删除表1中不存在的行
嘿,伙计们,需要你们的帮助,我很累,现在我累了,找不到出路 我有两个表&我确实想删除那些表2中没有外来id的行,下面我已经按结构提到了 表1Mysql 如何删除表1中不存在的行,mysql,sql,Mysql,Sql,嘿,伙计们,需要你们的帮助,我很累,现在我累了,找不到出路 我有两个表&我确实想删除那些表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 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。有什么办法吗
顺便说一下,糟糕的表名和列名
此处演示-此功能:
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
试试这个,工作
请尽你最大的努力,即使它没有产生正确的结果。至少我们会知道什么是不需要解释的。你确定你得到了OP想要的结果吗?@PM77-1谢谢你指出。现在请检查我已经更正了代码。
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
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