如果存在于B中,则从A中删除Sql

时间:2013-10-17 12:58:48

标签: mysql

我有两张桌子:

  • USER
  • MAILING

MAILING表有一列email

USER表包含许多users email

现在我要删除这样的内容:

DELETE email FROM mailing WHERE users.email = mailings.email 

因此,如果用户表中存在此电子邮件,我需要从表邮件中删除电子邮件。

4 个答案:

答案 0 :(得分:2)

您可以使用此

DELETE FROM mailing WHERE email IN (SELECT email FROM users)

或使用JOIN

DELETE M
FROM mailing M
  JOIN users U
    ON M.email = U.email 

答案 1 :(得分:0)

如果要更新列

,请尝试update而不是delete
UPDATE mailing 
inner join users on users.email = mailings.email  
set email=null 

或者您想要删除记录 试试这个:

DELETE m 
FROM mailing m 
inner join users u on u.email=m.email

答案 2 :(得分:0)

考虑到您要删除邮件表中的所有行,并且邮件表中只有一列。

Delete
from mailing where mailing.email 
exists (select email from users where users.email = mailing.email);

答案 3 :(得分:0)

您可以使用MySQL的 EXISTS 子句,如下所示 -

delete from mailing where exists(select * from user where user.email=mailing.email);

无需使用JOIN