删除mysql上的重复记录?

发布于 2024-10-28 18:12:52 字数 220 浏览 2 评论 0原文

我有这个 mysql 查询,可以查找重复项以及每个主题的出现次数:

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

但我想要做的是删除找到的所有重复项。我只想要每个主题有一个唯一的名称,并且不能重复!谢谢

I have this mysql query that finds duplicates and the number of occurances for each topic:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对不⑦ 2024-11-04 18:12:52
DELETE  t2
FROM    topics t1
JOIN    topics t2
ON      t2.name = t1.name
        AND t2.id < t1.id
DELETE  t2
FROM    topics t1
JOIN    topics t2
ON      t2.name = t1.name
        AND t2.id < t1.id
温柔少女心 2024-11-04 18:12:52

我会将所有唯一条目复制到一个新表中:

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

检查数据,然后在确定一切正常时删除旧表,并将新表重命名为旧表。

然后使名称列唯一,这样您就不必再次执行此操作。

干杯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文