如何删除没有临时表的 MySQL 表中的所有重复记录

How do I delete all the duplicate records in a MySQL table without temp tables(如何删除没有临时表的 MySQL 表中的所有重复记录)
本文介绍了如何删除没有临时表的 MySQL 表中的所有重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这方面的许多变体,但没有一个完全符合我想要完成的目标.

I've seen a number of variations on this but nothing quite matches what I'm trying to accomplish.

我有一个表格,TableA,其中包含用户对可配置问卷的回答.这些列是 member_id、quiz_num、question_num、answer_num.

I have a table, TableA, which contain the answers given by users to configurable questionnaires. The columns are member_id, quiz_num, question_num, answer_num.

不知何故,一些成员的答案被提交了两次.所以我需要删除重复的记录,但要确保留下一行.

Somehow a few members got their answers submitted twice. So I need to remove the duplicated records, but make sure that one row is left behind.

没有主要列,因此可能有两三行包含完全相同的数据.

There is no primary column so there could be two or three rows all with the exact same data.

是否有删除所有重复项的查询?

Is there a query to remove all the duplicates?

推荐答案

在你的表上添加唯一索引:

ALTER IGNORE TABLE `TableA`   
ADD UNIQUE INDEX (`member_id`, `quiz_num`, `question_num`, `answer_num`);

另一种方法是:

在您的表中添加主键,然后您可以使用以下查询轻松地从表中删除重复项:

Add primary key in your table then you can easily remove duplicates from your table using the following query:

DELETE FROM member  
WHERE id IN (SELECT * 
             FROM (SELECT id FROM member 
                   GROUP BY member_id, quiz_num, question_num, answer_num HAVING (COUNT(*) > 1)
                  ) AS A
            );

这篇关于如何删除没有临时表的 MySQL 表中的所有重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)