MySQL删除数据,其中字段包含在另一个表中

时间:2018-02-07 21:44:53

标签: mysql

我有一个包含2个这样的表的数据库(比这个大得多)

users
Name      |     Email
Bill            billy@gmail.com
Susan           susan@gmail.com

-

bad_emails
Email
billy@gmail.com
bademail@gmail.com

我想从电子邮件位于bad_emails表中的用户中删除任何内容。

我试过这样的事情

DELETE FROM users WHERE (SELECT Email FROM bad_emails) as bad_emails = users.Email

但这是一个语法错误,不确定正确的语法。

1 个答案:

答案 0 :(得分:1)

多种方式。

JOIN

DELETE u
FROM users AS u 
JOIN bad_emails AS b ON u.email = b.email

WHERE IN

DELETE FROM users
WHERE email in (SELECT email FROM bad_emails)

WHERE EXISTS

DELETE FROM users AS u
WHERE EXISTS (SELECT 1 FROM bad_emails AS b WHERE b.email = u.email)