0

I have two tables:

  • USER
  • MAILING

MAILING table has one column email.

USER table has many users with email.

Now I want to delete something like this:

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

So i need to delete email from table mailing if this email exist in user table.

Waldheinz
  • 10,399
  • 3
  • 31
  • 61
Wordica
  • 2,427
  • 3
  • 31
  • 51

4 Answers4

2

You can use this

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

or this using JOIN

DELETE M
FROM mailing M
  JOIN users U
    ON M.email = U.email 
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0

Try update not delete if you want to update a column

UPDATE mailing 
inner join users on users.email = mailings.email  
set email=null 

or you want to delete the records TRy this:

DELETE m 
FROM mailing m 
inner join users u on u.email=m.email
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
0

Considering you want to delete all rows from mailing table, and you've only one column in mailing table.

Delete
from mailing where mailing.email 
exists (select email from users where users.email = mailing.email);
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
0

You can use EXISTS clause of MySQL as follows-

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

No Need of using JOIN

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57