RonnieT
RonnieT

Reputation: 2203

MySQL - Delete Rows Not Being Used

I have two tables, events and locations. Each has a column named venue_id to match an event to the correct location. venue_id is not the primary key.

What query could I run to delete the rows in locations that have no event linked to them?

Upvotes: 0

Views: 71

Answers (2)

Mike Christensen
Mike Christensen

Reputation: 91666

Maybe something like:

DELETE FROM Locations where venue_id not in
  (select distinct venue_id from Events);

Upvotes: 1

James Browne
James Browne

Reputation: 186

This should do it :

DELETE FROM locations WHERE venue_id
NOT IN (SELECT DISTINCT venue_id FROM events)

Upvotes: 3

Related Questions