从表中删除记录,如果其特定值未出现在另一个表中
Posted
技术标签:
【中文标题】从表中删除记录,如果其特定值未出现在另一个表中【英文标题】:Remove records from table, if its specific value does not appears in another table 【发布时间】:2013-12-11 12:55:14 【问题描述】:我有两张桌子。 T1
有t1field1
和t1field2
,T2
有t2field1
和t2field2
。
如果t2field1
值在t1field1
中不存在,我想从T2
中删除所有记录
+----------+----------+
| t1field1 | t1field2 |
+----------+----------+
| 1 | x |
| 5 | y |
| 6 | z |
+----------+----------+
+----------+----------+
| t2field1 | t2field2 |
+----------+----------+
| 3 | x |
| 4 | y |
| 5 | z |
+----------+----------+
【问题讨论】:
【参考方案1】:试试这个:
DELETE t2
FROM t2 LEFT JOIN t1 ON t1.t1field1 = t2.t2field1
WHERE t1.t1field1 IS NULL
或
DELETE FROM t2 WHERE t2.t2field1 NOT IN (SELECT t1.t1field1 FROM t1)
【讨论】:
【参考方案2】:delete t2
from t2
left join t1 on t1.t1field1 = t2.field1
where t1.field1 is null
【讨论】:
【参考方案3】:试试这个
DELETE FROM T2
WHERE t2field1 NOT IN (SELECT t1field1 FROM T1)
【讨论】:
以上是关于从表中删除记录,如果其特定值未出现在另一个表中的主要内容,如果未能解决你的问题,请参考以下文章