timlentse

Remove duplicate rows from a table in mysql

This is a simple sql for removing duplicate rows from a table and keep one with lowest id

DELETE r1 FROM table_name r1, table_name r2 WHERE r1.id > r2.id AND r1.name = r2.name

Note: the name is the field name you want to ensure values are unique

Constraint unique

If you want to remove duplicate rows decided by more than one column

DELETE r1 FROM table_name r1, table_name r2 WHERE r1.id > r2.id AND r1.name1 = r2.name1 
and r1.name2=r2.name2 and ...

Reference