1

我想从 Table2 中删除我的行,其中 ID 存在于 Table1 中,并且在 Table1 中,确定是否应该发生删除的值驻留。

例子:

表格1

+--------+--------+
|   ID   | Author |
+--------+--------+
|  121   |  John  |
+--------+--------+
|  150   |  Ann   |
+--------+--------+

表2

+--------+---------+-----------+
|   ID   | MetaKey | MetaValue |
+--------+---------+-----------+
|  121   |  Color  |    red    |
+--------+---------+-----------+
|  150   |  Color  |    grey   |
+--------+---------+-----------+
|  121   |  Weight |    10     |
+--------+---------+-----------+
|  150   |  Weight |    30     |
+--------+---------+-----------+

并从表 2 中删除行,其作者是“约翰”(来自表 1)

4

2 回答 2

5

您可以使用exists子句和相关子查询来做到这一点:

delete from t2
where exists (select 1 from t1 where t2.id = t1.id and t1.Author = 'John')
于 2013-08-29T13:20:09.720 回答
3
delete table2
from table2
inner join table1 on table1.ID = table2.ID
where table1.author = 'John'
于 2013-08-29T13:19:45.250 回答