How to Delete from a Table where rows match in Another Table – MySQL

So you have a table where you want to delete a bunch of rows, based on a particular column being matched in another table.

This is easily achievable with MySQL.

DELETE FROM TABLE1
WHERE domain IN (
	SELECT domain FROM TABLE2
)

The above SQL will delete all rows in TABLE1 that are found in TABLE2.

But what if we need to limit which records are returned from TABLE2?

That’s simple too. All we need to do is add a traditional WHERE clause to our select statement.

DELETE FROM TABLE1
WHERE domain IN (
	SELECT domain FROM TABLE2
	WHERE column1='1' AND column2<>'2'
)

Tags:

Leave a Reply

Your email address will not be published.