Say I have table1 t1, and table t2, and they're related by t1.id=t2.rid. t1 has had rows deleted, so there are now excess rows in t2 that are related to non-existent rows in t1. how can i delete these excess rows in t2?
| SitePoint Sponsor |


Say I have table1 t1, and table t2, and they're related by t1.id=t2.rid. t1 has had rows deleted, so there are now excess rows in t2 that are related to non-existent rows in t1. how can i delete these excess rows in t2?
michael.Crabbe


Code:Delete from t2 where t2.rid not in (select t1.id from t1);


Say t2 and t1 both have about 9,000 rows.
This takes about 10 minutes to execute through phpMyAdmin. Should it take that long or should the tables perhaps have a more optimised structure?
Or is there a quicker way of doing this altogether?
michael.Crabbe


Added an index and took less than a second for several of those queries
Thanks.
michael.Crabbe
Bookmarks