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
February 2nd 2006 - jamil shaikh

Thanks ever so much guys

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

December 18th 2007 - shp

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

January 29th 2008 - neworld

Thanks a lot

March 17th 2008 - Nick Evans

<3

March 20th 2008 - Earnie

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

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

July 3rd 2008 - Andrew4fr

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

August 12th 2008 - Anand

Thank You, Boss

October 23rd 2008 - Mathias

Thank you SO (!) much for sharing this

November 13th 2008 - thanks

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

January 23rd 2009 - ben

Very helpful, thanks

February 25th 2009 - kamm

Thanks - useful info and worked for me...

April 25th 2009 - kixs

Worked for me also.. Thanks :P

May 29th 2009 - Jabop

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

April 23rd 2010 - Beauford

Doesn't work.

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

For me userid is CustID

October 15th 2010 - Beauford

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

October 25th 2010 - Egy

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

Works on MySQL 5.0.51a

July 4th 2011 - mr.magicsock

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

July 9th 2011 - Nate Andersen

Thanks for this tip!

December 24th 2011 - Andreka

makasih mas.. matur nuwun

January 18th 2012 - oranzo

Thanks :)

February 2nd 2012 - Aaron R

Big help, thanks for sharing!=)

February 13th 2012 - Don Clemento

It helped me a lot! Thank you guys!

June 10th 2012 - Saurabh

Thank you so much. It helps me a lot.

December 18th 2012 - apoca

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!

January 2nd 2013 - ben

Thanks

February 1st 2013 - kalai

good solution with explanation.thanks a lot

March 5th 2013 - steve

Thanks, works great!!

September 27th 2017 - eLo

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