SQL
Submitted by valerio on Tue, 02/02/2010 - 17:45
Search and delete duplicate records
SELECT email FROM users GROUP BY email HAVING ( COUNT(email) > 1 )
This works also for multiple columns
SELECT col1, col2,col3,count(col3)--put the last column of duplicate combination in Count() FROM table1 GROUP BY col1,col2,col3 HAVING COUNT(col3) >1
To delete one of the records if you have a primary key
CREATE TABLE tmptable SELECT min(primary_key) as primary_key, col1, col2,col3,count(col3)--put the last column of duplicate combination in Count() FROM table1 GROUP BY col1,col2,col3 HAVING COUNT(col3) >1 -- Now you have all the first duplicate record keys in tmptable DELETE FROM table1 where primary_key in (SELECT primary_key FROM tmptable) DROP TABLE tmptable
Repeat until nothing is selected
- Login to post comments