在另一个表上查找后从表中删除 - SQL

时间:2011-05-18 15:05:11

标签: mysql sql

HIII
我有两张桌子

Invited

  • UID [pk]

Active

  • UID [pk]

我想删除Invited表格中存在的Active的UID 我可以使用加入来执行此操作,或者唯一的方法是使用NOT IN or IN

2 个答案:

答案 0 :(得分:4)

是的,您可以使用IN

Delete From Invited where UID in (Select UID From Active)

如果您想

,也可以使用加入
Delete i
From Invited as i
Join Active as a on i.UID = a.UID 

答案 1 :(得分:2)

DELETE FROM `Invited` WHERE Invited.UID IN ( SELECT UID FROM `Active`)

或者,为了提高绩效:

DELETE i FROM `Invited` AS i WHERE EXISTS ( SELECT 1 FROM `Active` WHERE Active.UID = i.UID )