MySql delete duplicate records

MySql delete duplicate records

MySql delete(remove) duplicate rows(records)

We will show you how to remove duplicate rows in a MySql.

First we will create a table with some duplicate records. Below show a sample table image with duplicate records.

sample_table_duplicate_records
sample_table_duplicate_records

Here we run a query to know how many name column repeated.


SELECT *,count(name) AS dup_rows FROM `user_info` GROUP BY name
same_name_count
same_name_count

Above Image you can see that two duplicate records in table those dup_rows column count are more than 1.

Remove duplicate records use INNER JOIN with DELETE Here we delete same name values except one record. For achieve this result use below mention query statement


DELETE table1 FROM user_info AS table1 INNER JOIN user_info AS table2 WHERE table1.id < table2.id AND table1.name = table2.name;
Now run SELECT * FROM `user_info` Command

after_delete_result
after_delete_result

You can see on the above image no duplicate records available.

I hope this article helps you.
Thanks for visiting Inflay.com .