• home
  • forum
  • my
  • kt
  • download
  • Deleting/Identifying duplicate records

    Author: 2008-09-12 10:04:09 From:

    A simple way out to delete duplicate entries is to copy DISTINCT values to a temporary table. I tried it over 2 field table with 0.1 million rows and it worked perfectly for me.

    view plaincopy to clipboardprint?
    1. CREATE TABLE NewTable ...;  
    2.  
    3. INSERT INTO NewTable(field1)  
    4. SELECT DISTINCT field1 FROM MyTable;  
    5.  
    6. DROP TABLE MyTable; 

    Alternately, if you’re populating your database and would like to avoid duplicate entries, modify your INSERT queries to *REPLACE. For e.g.

    view plaincopy to clipboardprint?
    1. REPLACE INTO table1 (field1, .. , ..) VALUES ('value1', .. , ..) 

    In case you want to delete the duplicate records from the given table, you can try executing:

    view plaincopy to clipboardprint?
    1. Delete bad_rows.*  
    2. FROM MyTable AS bad_rows  
    3. INNER JOIN (  
    4. SELECT field1, MIN(id) AS min_id  
    5. FROM MyTable  
    6. GROUP BY field1  
    7. HAVING COUNT(*) > 1  
    8. AS good_rows ON good_rows.field1 = bad_rows.field1  
    9. AND good_rows.min_id <> bad_rows.id; 

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Miscellaneous (14)

    New

    Hot