MySQL Query to Delete Duplicate Rows from Table

Are you want to delete duplicate rows from MySQL table? Using one MySQL query, you can remove duplicate records from table. MySQL query for remove duplicate rows from the database table is given below.

Keep the Highest ID

The following query deletes all duplicate rows from the table and keeps the highest ID.

DELETE t1 FROM `users` t1, `users` t2 WHERE t1.id < t2.id AND t1.name = t2.name

Keeping the Lowest ID

The following query deletes all duplicate rows from the table and keeps the lowest ID.

DELETE t1 FROM `users` t1, `users` t2 WHERE t1.id > t2.id AND t1.name = t2.name

Leave a reply

keyboard_double_arrow_up