我有两张桌子。 T1
有t1field1
和t1field2
,T2
有t2field1
和t2field2
。
如果T2
t2field1
值,我想从t1field1
删除所有记录
+----------+----------+
| t1field1 | t1field2 |
+----------+----------+
| 1 | x |
| 5 | y |
| 6 | z |
+----------+----------+
+----------+----------+
| t2field1 | t2field2 |
+----------+----------+
| 3 | x |
| 4 | y |
| 5 | z |
+----------+----------+
答案 0 :(得分:3)
试试这个:
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)
答案 1 :(得分:1)
delete t2
from t2
left join t1 on t1.t1field1 = t2.field1
where t1.field1 is null
答案 2 :(得分:1)
试试这个
DELETE FROM T2
WHERE t2field1 NOT IN (SELECT t1field1 FROM T1)