I'm trying to clean up a table that has a lot of old data in it, about 4 million extra rows to be exact. I want to delete all rows from table1 where fieldA isn't matched in any of the rows of table2 in fieldB how would I write that?
Thanks!
| SitePoint Sponsor |




I'm trying to clean up a table that has a lot of old data in it, about 4 million extra rows to be exact. I want to delete all rows from table1 where fieldA isn't matched in any of the rows of table2 in fieldB how would I write that?
Thanks!


delete from table1
where not exists (select 1 from table2 where table2.fieldB = table1.fieldA)




I tried using that (using my actual field names) and I'm getting a syntax error?
DELETE FROM content_description WHERE NOT EXISTS (
SELECT 1
FROM content_links
WHERE content_links.resource = content_description.externalpage
)


you need to be on 4.1
i think you can also do it with a left outer join but i haven't tested that lately
check the manual for the multiple-table DELETE syntax




hmm....I have mysql 4.0.22 on my server, I guess I could upgrade it to 4.1
Bookmarks