1

I have a relational database (mysql) with two tables: Cities and Events .

Cities table:

   - city_id
   - city_name
   - city_coord

Events table

   - event_id
   - event_name
   - event_descr
   - city_id

In the Cities table I have 1500+ rows, and nearly 1300 rows aren't linked to the events table. How can I delete these rows?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Szántó Zoltán
  • 981
  • 1
  • 12
  • 26

2 Answers2

1

You can do:

DELETE FROM cities
WHERE city_id NOT IN (SELECT city_id FROM events)

As mentioned in the comments, if city_id can be null in events, this will not work. See this answer on why that is

Community
  • 1
  • 1
Filipe Silva
  • 21,189
  • 5
  • 53
  • 68
0
DELETE FROM Cities
WHERE city_id NOT IN (SELECT city_id 
                       FROM Events)
M.Ali
  • 67,945
  • 13
  • 101
  • 127