1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Removing Duplicate rows

Discussion in 'Databases' started by Nick_Mayhem, Dec 3, 2006.

  1. #1
    Hello friends,

    I have some problem here.

    I have one table in which there are number of duplicate entries. Some have 10 duplicate entries while some have 40 duplicates of it. The Primary key is the Auto-Increment ID.

    What I want to do is that remove all the duplicate ones and keep one instance of that entry.

    Is there any way that it can be done? I have tried some PHP scripts and all. But the loops and ways I have used are eating up more resources and it never completes.

    If there is a way to acomplish this in some less queries then it will be very good.

    Thanking You.
     
    Nick_Mayhem, Dec 3, 2006 IP
  2. fatmagoo

    fatmagoo Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
  3. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes there is. You can create a primary key of the id and something other unique in your entries which will remove the other duplicates (be careful you can only do this once).
     
    maiahost, Dec 3, 2006 IP
  4. Scolls

    Scolls Guest

    Messages:
    70
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    create table new_table (select distinct(field_with_duplicates),field1,field2,...fieldn from original_table)

    This will create a new table without the duplicates. This is much faster than performing deletes, which in any event fragment your table, and also that way no time is wasted on updating any indexes with each delete.
    Recreate your indexes afterwards. I haven't found a faster way yet. ;)
     
    Scolls, Dec 3, 2006 IP
    Nick_Mayhem likes this.
  5. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #5

    Thanks a lot :)

    Rep added.
     
    Nick_Mayhem, Dec 3, 2006 IP
  6. Scolls

    Scolls Guest

    Messages:
    70
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Pleasure! Thanks for the rep. :)
     
    Scolls, Dec 4, 2006 IP