Remove duplicated row

Discussion in 'MySQL' started by durgesh871, Jun 30, 2010.

  1. #1
    How can remove duplicate row in mysql
     
    durgesh871, Jun 30, 2010 IP
  2. NemoPlus

    NemoPlus Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,
    Please provide more information about what you perceive as a duplicate row (for instance, what is your table structure and which fields are the same for a duplicate row, table indexes, ...). Also, do you just want to remove those duplicates once, or do you want to prevent duplicates from occurring ever again?
    Kind regards,
     
    NemoPlus, Jul 1, 2010 IP
  3. netload

    netload Member

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Not quite right, but very simple solution:
    1. Get all duplicate rows
    2. Remove all superfluous rows in while cycle
    For example, if you have table filed "name" and you would remove dublicate rows for this field
    $result = mysql_query("select name, count(*) as c from your_table_name group by name having c>1");
    while ($row = mysql_fetch_array($result)) {
       mysql_query("delete from your_table_name where name='$row[name]' limit ".($row[c]-1));
    }
    PHP:
    And try to use unique index to prevent duplicate in future.
     
    netload, Jul 1, 2010 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    You can try following mysql command as well.

    ALTER IGNORE table_name ADD UNIQUE KEY `idx_column_name` (`column_name`);

    This will remove duplicate rows identified by column_name.
     
    mastermunj, Jul 13, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Hi,

    Adding unique key is good solution for cases when you need to run the query only once but if you need to do it without altering the table, please check this link : the example is more complex ( duplicates are calculated per site and per number of players ).
    Just try modifying it to your real table ( taking care about your real "duplicate" data definition ).
    Regards :)
     
    koko5, Jul 13, 2010 IP
  6. adamsinfo

    adamsinfo Greenhorn

    Messages:
    60
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #6
    You can either select it all into PHP and then process there to find duplicates, or, much cleaner/quicker:

    SELECT field FROM table GROUP BY field HAVING ( COUNT(field) > 1 )

    This will show you all items in 'table' where 'field' has duplicate results. If you're happy, you can replace with 'DELETE FROM table ...'
     
    adamsinfo, Jul 18, 2010 IP