W
W
webpixel2015-10-21 20:23:13
MySQL
webpixel, 2015-10-21 20:23:13

How to remove duplicates by ID in MySQL?

Hello! I want to remove duplicates on the site, since they are easy to find by ID:

SELECT id, COUNT(*) as count
FROM wp_posts
GROUP BY id
HAVING COUNT(*) > 1

Is it possible to somehow change the code so that instead of searching, it deletes 1 copy, leaving the original? Thanks a lot!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aleksey Ratnikov, 2015-10-21
@webpixel

Try like this:

CREATE table wp_posts_copy LIKE wp_posts;
ALTER TABLE wp_posts_copy ADD PRIMARY KEY (id);
INSERT INGORE INTO wp_posts_copy SELECT * FROM wp_posts; -- INSERT INGORE вставит только по одному экземпляру id.
DROP TABLE wp_posts;
RENAME TABLE wp_posts_copy TO wp_posts;

I
Ivanq, 2015-10-21
@Ivanq

sqlinfo.ru/forum/viewtopic.php?id=274
MYSQL. Remove duplicate lines
And always...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question