Help with query to remove all records where foreign key doesn't have corresponding records

2008-08-13 Thread Daevid Vincent
I want to remove all records from 'feed_tag' where the feed_id foreign key doesn't have any corresponding records in feed. For instance I may have a record in feed_tag that is like (23, 10, 4543, '... (some date)'). Then lets say there is no record in feed that has a primary id key of 10. I

Re: Help with query to remove all records where foreign key doesn't have corresponding records

2008-08-13 Thread Isart Montane
Hi Daevid If you are using a foreign key you can set the reference as cascade and when a row is deleted from feed it will be deleted from feed_tag. http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html If you don't like it you can delete it easy with a query like this

Re: Help with query to remove all records where foreign key doesn't have corresponding records

2008-08-13 Thread Brent Baisley
Just do a left join with the delete query. DELETE feed_tag FROM feed_tag LEFT JOIN feed ON feed_tag.feed_id=feed.id WHERE feed.id IS NULL That should do it. You can change DELETE feed_tag to SELECT and test it first. -- Brent Baisley On Aug 13, 2008, at 4:51 PM, Daevid Vincent wrote: