tjmbc Posted June 8, 2008 Share Posted June 8, 2008 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2008 Share Posted June 8, 2008 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) Quote Link to comment Share on other sites More sharing options...
tjmbc Posted June 9, 2008 Author Share Posted June 9, 2008 That worked perfect, but forgive me for saying I'm not sure how to replace the original table with the 'tmp' one. Anyone? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 9, 2008 Share Posted June 9, 2008 backup original table first (just in case) mysql_query ("TRUNCATE TABLE original"); // removes all records mysql_query ("INSERT INTO original SELECT * FROM tmp"); // move from rmp to original table Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.