Back

Find and Remove Duplicates in MySQL

Volodymyr Hodiak
Volodymyr Hodiak
June 13, 2019 5 min read 820 views
June 13, 2019 5 min read 820 views
image

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.

POSTED IN:

How about to rate this article?

  • 1
  • 2
  • 3
  • 4
  • 5

RELATED ARTICLES

Explore all articles Explore all articles

let`s get in touch!

    By submitting this form I consent to processing my personal data as described in the Privacy Policy

    Success!
    Thanks for your message.

    While we are processing your request, don’t hesitate and follow us on social networks!

    Michael Askew

    CEO, ASKWHO

    Simon Nilsson

    Co-CEO, Work-Wide Group

    Dr. Arne-Rasmus Draeger

    Executive, ACARiS

    Samuel Bleakly

    Marketing Director, Coto World

    Philip Ly

    CEO, LES Automotive

    Vance Heron

    CTO, PeteHealth

    Tobias Sturesson

    CEO, Fervent

    Alex Reizer

    Product Manager, Kadabra

    Shlomie Singer

    Co-Founder & Owner at uBranded

    Artyom Popov

    Co-Founder at Escape Room

    Michal Nowakowski

    Founder in a Castles.today

    Alex Ostroverkh

    CEO at PettersonApps

    Mariya Osnovina

    Brand Manager – Intersono IVF clinic

    Waleed Kharma

    Managing Director, Foxtrapp

    Pavlo Lysy

    CEO & Founder, Panem