Remove duplicate records using mysql query
As a developer we always require to remove duplicate field values from our db tables. Some time we are in hurry and we don’t want to do brain storming to find out the solution of this question. So i have decided to write these special sql query so that we can get this solution easily.
In my example i have taken a dummy table named ’email’ which have only two field. 1. Id (auto incremented) 2. Email (customers email)
Mysql query to find duplicate record count into the database
SELECT *, count(*) as n FROM email group by email HAVING n>1
id email n
15 balwanmehra <[email protected]>, 2
16 dkkhosla <[email protected]>, 2
Mysql query to remove duplicate field value from a database table
To achieve this we will copy all unique records in a new tables by below mysql query
INSERT INTO email_new (email) SELECT email FROM email group by email HAVING count(*)>0
now delete old table using below query
drop table email
rename new table with old table name by using below query
rename table email_new to email;
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments