Elliot Haughin

Weblife

Thoughts

  • About to pay for moblie me. Absolutely love the service so far. Will be well worth it!
  • Time for bed. I'll forget I've changed my hair and get a shock when I look in the mirror tomorrow!
  • Hair finished, and it's very very black!
  • Dying my hair. First time I've done by own. Hope it comes out ok. I'll see in 30 minutes!
  • Down the pub with the church crew. It's strange discussing the preach over a cold pint!
  • Anyone going to the 5pm service at hillsong London? It's gonna be great!
  • The perfect night. Lots to drink, house party with friends, no hangover and a fried breakfast. Brilliant!
  • Phoned dell about my fried graphics card in my xps. This warranty is great! Engineer coming to my work to replace the card on Tuesday!

Tunes

  • 19 hours agoSwitchfoot – Meant to Live
  • 19 hours agoJet – Hold On
  • 19 hours agoTrain – Ordinary
  • 19 hours agoDashboard Confessional – Vindicated
  • 21 hours agoMe First and the Gimme Gimmes – She Believes In Me (Kenny Rogers)
  • 21 hours agoMe First and the Gimme Gimmes – On The Road Again (Willie Nelson)
  • 21 hours agoMe First and the Gimme Gimmes – Goodbye Earl (Dixie Chicks)
  • 21 hours agoMe First and the Gimme Gimmes – San Francisco
  • 21 hours agoMe First and the Gimme Gimmes – My Boyfriend's Back
  • 21 hours agoMe First and the Gimme Gimmes – Blowin' in the Wind

MySQL: DELETE across multiple tables using JOIN.

Posted by Elliot on Thursday, November 1st, 2007

Ever had a situation where you’ve wanted to say delete rows from lots of tables in your database that can all be associated with some form of join?
Well, after a few minutes of playing around, I’ve found a nice way to do this in just 1 query:

DELETE u, up, upc, ut, utp
FROM users AS u
LEFT JOIN users_photos AS up ON up.user_id = u.id
LEFT JOIN users_photos_comments AS upc ON upc.photo_id = up.id
LEFT JOIN users_topics AS ut ON ut.user_id = u.id
LEFT JOIN users_topic_posts AS utp ON utp.topic_id = ut.id
WHERE u.id = 12

The code is pretty straight forward, and simple to use.

Posted in: dev, sql.

9 Responses to “MySQL: DELETE across multiple tables using JOIN.”

  1. Hostingx Says:

    I have never managed to get more then 1 join ¬¬ how dit you get multiple joins in 1 query :P you bastard ^^

  2. MiniVip Says:

    Hi,
    that’s what i was lookin’ for. Unfortunately it doesn’t works on older mysql (3.23x) as i have. I’m getting mad to find a workaround… but i’m afraid the older versions cannot permit left joins within delete queries. :(

  3. justy Says:

    Ahhhhh, great! :D
    Thank you very much, that mysql manual is not really helpful in that situation!

  4. Spacebiker Says:

    Than u VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VEEEEEEEEEEEEEERY MUCH!!!!
    U’re genius!!!

  5. HardcoreProgrammer Says:

    Thank you…this little gem has saved me hours of work experimenting to see if it could be done.

  6. krishna Says:

    Really It’s helpful and useful query.

  7. Caleb Says:

    What’s the deletion order? Does it delete in the order the tables were specified in the FROM stanza? Or does MySQL figure it out itself? If you have foreign key dependencies deletion order is important.

  8. Caleb Says:

    Hmmm…. it looks like MySQL may not delete in the correct parent-child order specified by your foreign keys:

    From http://dev.mysql.com/doc/refman/5.1/en/delete.html:
    “If you use a multiple-table DELETE statement involving InnoDB tables for which there are foreign key constraints, the MySQL optimizer might process tables in an order that differs from that of their parent/child relationship. In this case, the statement fails and rolls back. Instead, you should delete from a single table and rely on the ON DELETE capabilities that InnoDB provides to cause the other tables to be modified accordingly.”

  9. Marco Fang Says:

    This query is fantastic, but it won’t work if there are foreign key constraints. What’s your suggestion to do with constraints?

Leave a Reply

Outside ChurchOutside ChurchOutside ChurchOutside ChurchOutside ChurchOutside ChurchOutside ChurchHouse PartyHouse PartyHouse PartyHouse PartyHouse PartyHouse PartyHouse PartyHouse PartyHouse PartyHouse PartyHouse Party