MySQL Delete with a LEFT JOIN
A database I was working on had some orphanned records that I was able to see using a LEFT JOIN.
SELECT EventID FROM EventLog e LEFT JOIN CustInfo c USING (CustID) WHERE c.CustID IS NULL;
Deleting those records is a little more complicated that changing the SELECT to DELETE. Specifically you have to tell MySQL what table you want to delete the records from (i.e. Eventlog.*) otherwise it gets confused about what table to delete the records from.
DELETE e.* FROM EventLog e LEFT JOIN CustInfo c USING (CustID) WHERE c.CustID IS NULL;
Information borrowed from ElectricToolbox.
Replies
Thanks ever so much guys
the delete query works wonders and saved my job!!!
Thank you to you too!! I google this problem and It is really work with my job.
Thanks a lot
<3
I had to use the form of but thanks for the information:
DELETE e.* FROM EventLog e LEFT JOIN CustInfo c USING (CustID) WHERE c.CustID IS NULL;
Yup use the short name for the table (e) instead of the full name (EventLog), and you did it. Thanks for the help!
Also works DELETE EventLog FROM EventLog e LEFT JOIN CustInfo c USING (CustID) WHERE c.CustID IS NULL;
Thank You, Boss
Thank you SO (!) much for sharing this
does not work in wright way but thank you all the same !
Very helpful, thanks
Thanks - useful info and worked for me...
Worked for me also.. Thanks :P
worked for me also, but the table you're deleting from must be referenced by its alias and not the full table name, otherwise you'll this error:
ERROR 1109 (42S02): Unknown table 'TABLE_NAME' in MULTI DELETE
Doesn't work.
Error Code : 1054 Unknown column 'userid' in 'from clause'
For me userid is CustID
Still Nothing, and I have tried numerous code based on what has been said here, so I must assume this can't be done in the version of MySQL I am using, which is 5.x. Anyone????
Here are a couple I tried:
DELETE v. FROM poll_votes v LEFT JOIN polls p USING (poll_id) WHERE v.poll_id = p.id; DELETE v. FROM poll_votes v LEFT JOIN polls p USING (id) WHERE v.poll_id = p.id; DELETE poll_votes. FROM poll_votes v LEFT JOIN polls p USING (poll_id) WHERE v.poll_id = p.id; DELETE poll_votes FROM poll_votes v LEFT JOIN polls p USING (poll_id) WHERE v.poll_id = p.id; DELETE FROM poll_votes v LEFT JOIN polls p USING (poll_id) WHERE v.poll_id = p.id;
Anyone help out with the right syntax as I have no idea!!
Thanks
DELETE tag.* FROM tag LEFT JOIN photo ON tag.photo = photo.photo_id WHERE photo_id IS NULL;
Works on MySQL 5.0.51a
you saved my life and my job, and my wife and kids too YOU'RE AWESOME.
Thanks for this tip!
makasih mas.. matur nuwun
Thanks :)
Big help, thanks for sharing!=)
It helped me a lot! Thank you guys!
Thank you so much. It helps me a lot.
Thxs, good to know this, days before the end of the world, I'm currently learning as much as possible to die as a smart man!
Thanks
good solution with explanation.thanks a lot
Thanks, works great!!
As Earnie I had to add e. instead of EventLog. and worked great, thanks for the ease!