我有一个关系数据库(mysql),有两个表:Cities和Events。
城市表:
- city_id
- city_name
- city_coord
活动表
- event_id
- event_name
- event_descr
- city_id
在Cities表中,我有1500多行,并且近1300行没有链接到事件表。如何删除这些行?
答案 0 :(得分:1)
你可以这样做:
DELETE FROM cities
WHERE city_id NOT IN (SELECT city_id FROM events)
如评论中所述,如果city_id在事件中可以为null,则无效。 请参阅this answer了解原因
答案 1 :(得分:0)
DELETE FROM Cities
WHERE city_id NOT IN (SELECT city_id
FROM Events)