Find and Remove Duplicates in MySQL

image
Volodymyr Hodiak
June 13, 2019|5 min read|1047 views

Once we faced a problem. We received a ticket from our client, “Can't update the price of the 63072 m item”. This is an e-commerce website with automatic data import in xls format. We built this site a long time ago.

Looking for the solution, I noticed that some items in the database are duplicated. I found out that there is no unique index in the product identification number column. But now we can't just put it there since we have duplicate records.

Solution:
Create a temporary table and write unique identification numbers of the items in it, then delete the duplicate records from the main table.

Here's the code.


CREATE TEMPORARY TABLE `p_temp`
  as  (
    SELECT min(product_id) as id
    FROM `wm_products`
    GROUP BY product_code
  );

DELETE from `wm_products`
WHERE `wm_products`.product_id not in (
  SELECT id FROM p_temp
);

ALTER TABLE `wm_products` ADD UNIQUE (
  `product_code`
);

In this way, I removed the unnecessary stuff from the table. Then I found a mistake in the script that imports the items and fixed it.

Hope the article will be useful for you.

Share

Related articles

image
August 10, 2022|9 min read
Healthcare Management System (HMS): Features and Benefits
HMS (Healthcare Management System) AKA Hospital Management Upgrade Over the last few decades, the need to digitize the healthcare services industry has increased immensely. Having a system in plac...
image
July 29, 2022|10 min read
What’s New in Flutter 3.0? The Ultimate Guide For You
Flutter 3 release. What's new? Oh, you’ve heard it right! The new massive Flutter update is here, and it’s world-shaken the whole app development industry. Release of Flutter has been a rollercoas...
image
January 4, 2020|2 min read
How to Remove .idea Directory from .git
How to Remove .idea Directory from .git If you have accidentally added the .idea directory to .git repository, and you don’t want it to be there, then there is an easy way to remove it. You just h...
Interested in other posts?

CONTACT US

By submitting this form I consent to processing my personal data as described in thePRIVACY POLICY

We are open to answer you directly

image
image

Serhiy Lavrynenko

Volodymyr Hodiak

CEO

CTO