SQL command to remove duplicate records

Discussion in 'MySQL' started by globalcashsite, Jan 7, 2010.

  1. #1
    Guys

    I have one table name "address" with only one field "email" and I wish to remove all duplicate email addresses from this table.

    Please help me and give me command to do this?

    Thank you

    GCS
     
    globalcashsite, Jan 7, 2010 IP
  2. zro

    zro Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This will only work if you have a unique id field

    first creates a backup

    
    SELECT email
    FROM address a
    WHERE id > ( SELECT min(id)
    FROM address b
    WHERE b.email = a.email );
    
    Code (markup):

    
    DELETE email
    FROM address a
    WHERE id > ( SELECT min(id)
    FROM address b
    WHERE b.email = a.email );
    
    Code (markup):
     
    zro, Jan 7, 2010 IP
  3. basia

    basia Well-Known Member

    Messages:
    263
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    155
    #3
    You could also add a unique index on the email field so that you won't get duplicate addresses going forward.
     
    basia, Jan 7, 2010 IP
  4. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In fact I imported around 10,000 email addresses from text file and now I wish to delete duplicates, right now I have only one field to simply process. Once duplicates are removed then I will add more fields. :)

    GCS
     
    globalcashsite, Jan 7, 2010 IP
  5. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #5
    deleting duplicates wont be easy for large amount of data coz u have to individually check each email and tally it with all the records ..
     
    Bohra, Jan 8, 2010 IP
  6. Whizkid

    Whizkid Well-Known Member

    Messages:
    324
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    135
    #6
    An easy way to do this is to create a new table and copy the distinct values (values without their duplicates) from the old table into a new one:

    select distinct *[FONT=monospace]
    [/FONT]into NewEmails[FONT=monospace]
    [/FONT]from Emails
    Code (markup):
    This will remove all the duplicate emails in your list. Then you can go ahead and delete the table you currently have and use the new one you just copied the values into.
     
    Whizkid, Jan 8, 2010 IP