SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    root
    Join Date
    Oct 2003
    Location
    Tampa Bay
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I do this?

    I'm trying to clean up a table that has a lot of old data in it, about 4 million extra rows to be exact. I want to delete all rows from table1 where fieldA isn't matched in any of the rows of table2 in fieldB how would I write that?

    Thanks!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    delete from table1
    where not exists (select 1 from table2 where table2.fieldB = table1.fieldA)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    root
    Join Date
    Oct 2003
    Location
    Tampa Bay
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried using that (using my actual field names) and I'm getting a syntax error?

    DELETE FROM content_description WHERE NOT EXISTS (
    SELECT 1
    FROM content_links
    WHERE content_links.resource = content_description.externalpage
    )

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    you need to be on 4.1

    i think you can also do it with a left outer join but i haven't tested that lately

    check the manual for the multiple-table DELETE syntax
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    root
    Join Date
    Oct 2003
    Location
    Tampa Bay
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm....I have mysql 4.0.22 on my server, I guess I could upgrade it to 4.1


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
  •