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
  • 34,719
  • 24
  • 185
  • 251
Szántó Zoltán
  • 953
  • 11
  • 25

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
  • 20,688
  • 4
  • 50
  • 67
0
DELETE FROM Cities
WHERE city_id NOT IN (SELECT city_id 
                       FROM Events)
M.Ali
  • 65,124
  • 12
  • 92
  • 119