Results 1 to 4 of 4

Thread: How to Remove Duplicate Entries from Table ?

  1. #1
    Join Date
    Feb 2009
    Posts
    69

    How to Remove Duplicate Entries from Table ?

    There are certain entries in the database table that already exists. These entries are added more than once for some reason. How do I remove/delete these duplicate entries from the database table ?

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to Remove Duplicate Entries from Table ?

    This is an extremely quick and painless way to remove duplicate rows (tuples) from a MySQL database table. The best part of it is that it requires no programming or PHP coding whatsoever, it can all be done with three manual SQL queries :

    Step 1: move the non duplicates (unique tuples) into a temporary table
    CREATE TABLE new_table AS
    SELECT * FROM old_table WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];

    Step 2: delete delete the old table
    We no longer need the table with all the duplicate entries, so drop it!
    DROP TABLE old_table;

    Step 3: rename the new_table to the name of the old_table
    RENAME TABLE new_table TO old_table;

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to Remove Duplicate Entries from Table ?

    Before removing duplicate entries from the database table, first you need to find the duplicates(duplicate entries).

    There is a handy query for finding duplicates in a table.
    Suppose you want to find all email addresses in a table that exist more than once:
    SELECT email,
    COUNT(email) AS NumOccurrences
    FROM users
    GROUP BY email
    HAVING ( COUNT(email) > 1 )


    You could also use this technique to find rows that occur exactly once:
    SELECT email
    FROM users
    GROUP BY email
    HAVING ( COUNT(email) = 1 )

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to Remove Duplicate Entries from Table ?

    Apart from using primary key of the table and putting queries using primary key to remove duplicate entries from table, there is another approach that you can use by creating a second table with a primary key or unique constraint on that column, and insert into the new table all the rows from the old table.
    For each unique value, the first insert will succeed but all subsequents will fail due to violation of key or unique constraint.

    Once you are done, you can rename the old table to something else, and rename the new table with the old name.

Similar Threads

  1. Replies: 5
    Last Post: 24-01-2012, 05:02 PM
  2. Removing duplicate entries from an array in VB
    By McKenzie! in forum Software Development
    Replies: 6
    Last Post: 27-07-2011, 11:47 AM
  3. Duplicate Calendar Entries
    By ksharkman in forum Windows Software
    Replies: 2
    Last Post: 08-04-2011, 12:36 AM
  4. Replies: 2
    Last Post: 24-04-2009, 11:13 PM
  5. Scavenging or something else to cleanup duplicate DNS entries?
    By User Name in forum Windows Server Help
    Replies: 4
    Last Post: 06-10-2008, 07:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,730,251.94838 seconds with 17 queries