ascending.taurus avatar

How to remove duplicate records in mysql

ascending.taurus

Published: 16 Dec 2018 › Updated: 16 Dec 2018How to remove duplicate records in mysql

How to remove duplicate records in mysql

Today when fix bug for a client I found one of its database table has duplicate records and that cause plugin error.

See the following screenshot the pdf_id and cat_id fields there are duplicate records.

alt

At first I thought it may delete records by checking id is odd or even but I gave up it as I found there are other new records which has no duplicate ones.

alt

After some leaning I use the following sql to remove duplicate records in mysql and it worked as I want.

Step 1, query duplicate records

Use the following sql can query out all duplicate records

{code type=php}

SELECT MAX(id) FROM wp_bsk_pdf_manager_relationships GROUP BY pdf_id HAVING COUNT(pdf_id) > 1

{/code}

Step 2, create a temp table to store duplicate records id

It need to create a temp table to store duplicate records id because if the source id to remove also are target id.
{code type=php}

CREATE TABLE wp_temp(id INT);

INSERT INTO wp_temp(id) SELECT MAX(id) FROM wp_bsk_pdf_manager_relationships GROUP BY pdf_id HAVING COUNT(pdf_id) > 1;

{/code}

Step 3, remove duplicate records


{code type=php}

DELETE FROM wp_bsk_pdf_manager_relationships WHERE id IN( SELECT * FROM wp_temp )

{/code}

Step 3, remove temp table


{code type=php}
DROP TABLE wp_temp
{/code}

Now the duplicate records have been removed from mysql.

alt


Posted from my blog with SteemPress : https://waytowp.com/how-to-remove-duplicate-records-in-mysql/

Leave How to remove duplicate records in mysql to:

Written by

Read more #utopian-io posts


Best Posts From ascending.taurus

We have not curated any of ascending.taurus's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From ascending.taurus