Delete Duplicate Rows

Discussion in 'MySQL' started by kieranrobo, Jun 26, 2011.

  1. #1
    Something went wrong in my mysql script now I have loads of duplicate rows. Is there a way I can delete all duplicate rows and leave only one of the duplicates remaning using PHP or raw MySQL?
     
    kieranrobo, Jun 26, 2011 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    How do you identify that a row is duplicate? A simple search on Google returned a lot of relevant articles, just look here.
     
    mwasif, Jun 26, 2011 IP
  3. kieranrobo

    kieranrobo Peon

    Messages:
    254
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I identify the duplicated using the `domainname` coloum. If you look right at your google search, all of those delete ALL of the duplicates, and do not leave any of them. That why I came here.
     
    kieranrobo, Jun 26, 2011 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Hopefully you have an autonumber primary key which you can use to uniquely identify each row. If so, you would then create an aggregate sub-query which would have 2 fields--domainname which you group by and then the primary key field which you get the Min/Max/First of. This sub-query will be the records you keep. Next, you take your main table and left join to that sub-query and only return records that are not in the sub-query (primary key field is null). This query will identify which rows you will delete.

    As always, backup your data before attempting any action queries.
     
    plog, Jun 27, 2011 IP
  5. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you use MySQL workbench to manage your database tables, there is an option to force unique columns. You can choose/customize from there.
     
    sirgogo, Jun 30, 2011 IP