SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: DELETE from multiple tables at once?

  1. #1
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DELETE from multiple tables at once?

    How would this be done? The MYSQL manual isnt very clear and is confusing me more about other things. (Heck I didn't know you could even set the priority of a query! ) I want to delete all rows from 3 different tables where the column id (present in each table) equals something.

    Something along the lines of (I know the syntax is wrong)

    Code:
    DELETE * FROM table1, table2, table3 WHERE id='$id'"
    Cheers

    Dan

  2. #2
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think that you have to be using MySQL v4 and above, but you're query should work, and probably do what you expect. Delete all rows from all three tables where the idfield is $id. Just make sure each table has an id field

  3. #3
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you're using phpMyAdmin, throw that sql line in the sql box and see what phpMyAdmin says. If it doesn't say anything make sure $id is indeed being printed to the query string.

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should error about not knowing which table ID is for, eh?

    I think you have to have multiple where's, one for each table.

    Just a thought, because on selects you do

  5. #5
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the thoughts guys. It didnt seem to like the * in that statement and threw an " you have an error in your syntax enar...".

    Anyhoo, there are only 3 tables with about 10 rows in all to delete each time the action is called so I decided to do it with 3 different DELETE statements.

    Cheers

    Dan

  6. #6
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    It should error about not knowing which table ID is for, eh?
    I think you have to have multiple where's, one for each table.
    Just a thought, because on selects you do [img]images/smilies/wink.gif[/img]
    So what would be the proper query to do what he's looking for? Would this work

    PHP Code:
    DELETE FROM table1table2table3
    WHERE table1
    .id=$id OR table2.id=$id OR table3.id=$id 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •