开发者

deleting duplicate records on mysql?

I have this mysql query that finds duplicates and the number of occurances for each topi开发者_开发问答c:

SELECT name, 
 COUNT(name) AS NumOccurrences
FROM topics
GROUP BY name
HAVING ( COUNT(name) > 1 )

but what I want to do is delete all the duplicates that are found. I only want one unique name for each topic, and no duplicates!! thanks


DELETE  t2
FROM    topics t1
JOIN    topics t2
ON      t2.name = t1.name
        AND t2.id < t1.id


I would copy all the unique entries to a new table:

CREATE TABLE new_table as
SELECT * FROM old_table WHERE 1 GROUP BY unique_column_name;

Check the data, then delete your old table when you're sure everything's good and rename the new table to the old one.

Then make the name column unique so you won't have to do this again.

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜