How To Delete Duplicate Rows in MySQL

Delete duplicate or same records from database table its a very common task ,If you want delete duplicate or common  records or rows form your database table then you can go with below simple query.

1. Create tbl_user



2. If you want to keep the row with the lowest id value:
DELETE a FROM tbl_users a INNER JOIN tbl_users b ON a.username = b.username WHERE a.id > b.id
Output will be




3. If you want to keep the row with the highest id value:
DELETE a FROM tbl_users a INNER JOIN tbl_users b ON a.username = b.username WHERE a.id < b.id
Output will be




3. When I did it, I found that unless I also included AND n1.id <> n2.id, it deleted every row in the table.

0 تعليقات

إرسال تعليق

Post a Comment (0)

أحدث أقدم