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.

Leave A Reply - 29 Replies
Replies
jamil shaikh February 2nd 2006

Thanks ever so much guys

the delete query works wonders and saved my job!!!

shp December 18th 2007

Thank you to you too!! I google this problem and It is really work with my job.

neworld January 29th 2008

Thanks a lot

Nick Evans March 17th 2008

<3

Earnie March 20th 2008

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;
as if March 24th 2008

Yup use the short name for the table (e) instead of the full name (EventLog), and you did it. Thanks for the help!

Andrew4fr July 3rd 2008

Also works DELETE EventLog FROM EventLog e LEFT JOIN CustInfo c USING (CustID) WHERE c.CustID IS NULL;

Anand August 12th 2008

Thank You, Boss

Mathias October 23rd 2008

Thank you SO (!) much for sharing this

thanks November 13th 2008

does not work in wright way but thank you all the same !

ben January 23rd 2009

Very helpful, thanks

kamm February 25th 2009

Thanks - useful info and worked for me...

kixs April 25th 2009

Worked for me also.. Thanks :P

Jabop May 29th 2009

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

Beauford April 23rd 2010

Doesn't work.

Error Code : 1054 Unknown column 'userid' in 'from clause'

For me userid is CustID

Beauford October 15th 2010

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

Egy October 25th 2010

DELETE tag.* FROM tag LEFT JOIN photo ON tag.photo = photo.photo_id WHERE photo_id IS NULL;

Works on MySQL 5.0.51a

mr.magicsock July 4th 2011

you saved my life and my job, and my wife and kids too YOU'RE AWESOME.

Nate Andersen July 9th 2011

Thanks for this tip!

Andreka December 24th 2011

makasih mas.. matur nuwun

oranzo January 18th 2012

Thanks :)

Aaron R February 2nd 2012

Big help, thanks for sharing!=)

Don Clemento February 13th 2012

It helped me a lot! Thank you guys!

Saurabh June 10th 2012

Thank you so much. It helps me a lot.

apoca December 18th 2012

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!

ben January 2nd 2013

Thanks

kalai February 1st 2013

good solution with explanation.thanks a lot

steve March 5th 2013

Thanks, works great!!

eLo September 27th 2017

As Earnie I had to add e. instead of EventLog. and worked great, thanks for the ease!

All content licensed under the Creative Commons License