Jump to content

Removing duplicates


tjmbc

Recommended Posts

How do I delete duplicate records that have unique keys?

 

For instance, in my table there is:

 

id | value1 | value2 | value3

1      a          b          c

2      a          b          c

 

I want to delete record 2 (or 1).

I suppose I should also mention that there are a few thousand records in the table.

Link to comment
Share on other sites

CREATE TABLE tmp

SELECT * FROM tablename

GROUP BY val1,val2,val3

 

Then replace original table data with that from tmp

 

mysql> SELECT * FROM tests;
+----+------+------+------+
| id | val1 | val2 | val3 |
+----+------+------+------+
|  1 | a    | b    | c    |
|  2 | b    | c    | a    |
|  3 | a    | b    | c    |
|  4 | b    | c    | a    |
|  5 | d    | e    | f    |
|  6 | g    | h    | k    |
+----+------+------+------+
6 rows in set (0.00 sec)

mysql> CREATE TABLE tmp
    -> SELECT * FROM tests
    -> GROUP BY val1,val2,val3;
Query OK, 4 rows affected (0.17 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM tmp;
+----+------+------+------+
| id | val1 | val2 | val3 |
+----+------+------+------+
|  1 | a    | b    | c    |
|  2 | b    | c    | a    |
|  5 | d    | e    | f    |
|  6 | g    | h    | k    |
+----+------+------+------+
4 rows in set (0.00 sec)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.