In this tutorial of mysql, we will show you how to delete duplicate entries from MySql database table.
For example, we have a database table newsfeed, where we are saving news feed from news channel API. The data we receive from the API has uuid, title, description, published_at etc.
So to keep unique news feed we need to unique uuid, if a uuid already exists, lets not our cron job add the same news to the database. But if it happened mistakenly, then to delete the duplicate entries and keep the latest ones only, we can do it with the following example.
Table name: newsfeed
DELETE n1 FROM newsfeed n1 INNER JOIN newsfeed n2 WHERE n1.id > n2.id AND n1.uuid = n2.uuid;
Table name: User
In database table User, we will try to delete duplicate emails.
DELETE u1 FROM User u1 INNER JOIN User u2 WHERE u1.id > u2.id AND u1.email= u2.email;