MySQL删除重复记录但保留最新记录
我有独特的 id
和 email
字段。电子邮件会重复。我只想保留所有重复项的一个电子邮件地址,但包含最新的 id
(最后插入的记录)。
我怎样才能实现这个目标?
I have unique id
and email
fields. Emails get duplicated. I only want to keep one Email address of all the duplicates but with the latest id
(the last inserted record).
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
假设您的表
test
包含以下数据:因此,我们需要找到所有重复的电子邮件并将其全部删除,但最新的 id 除外。
在本例中,
aaa
、bbb
和eee
是重复的,因此我们要删除 ID 1、7、2 和 6。要实现此目的,首先我们需要找到所有重复的电子邮件:
然后,从这个数据集中,我们需要找到每一封重复电子邮件的最新 ID:
最后,我们现在可以删除所有 Id 小于 LASTID 的电子邮件。所以解决方案是:
我现在没有在这台机器上安装 mySql,但应该可以
更新
上面的删除可以工作,但我找到了一个更优化的版本:
您可以看到它删除了最旧的重复项,即 1, 7, 2、6:
另一个版本,是 Rene Limon 提供的删除
Imagine your table
test
contains the following data:So, we need to find all repeated emails and delete all of them, but the latest id.
In this case,
aaa
,bbb
andeee
are repeated, so we want to delete IDs 1, 7, 2 and 6.To accomplish this, first we need to find all the repeated emails:
Then, from this dataset, we need to find the latest id for each one of these repeated emails:
Finally we can now delete all of these emails with an Id smaller than LASTID. So the solution is:
I don't have mySql installed on this machine right now, but should work
Update
The above delete works, but I found a more optimized version:
You can see that it deletes the oldest duplicates, i.e. 1, 7, 2, 6:
Another version, is the delete provived by Rene Limon
试试这个方法
Try this method
正确的方法是
Correct way is
如果您想保留具有最低 id 值的行:
如果您想保留具有最高 id 值的行:
或者此查询也可能有帮助
If you want to keep the row with the lowest id value:
If you want to keep the row with the highest id value:
or this query might also help
修订版和工作版!!!谢谢@Gaurav
revised and working version!!! thank you @Gaurav
我个人对投票最高的两个答案遇到了麻烦。这不是最干净的解决方案,但您可以利用临时表来避免 MySQL 通过连接同一张表进行删除时出现的所有问题。
I personally had trouble with the top two voted answers. It's not the cleanest solution but you can utilize temporary tables to avoid all the issues MySQL has with deleting via joining on the same table.
我必须说,优化版本是一段甜蜜、优雅的代码,即使在 DATETIME 列上执行比较,它也能像魅力一样发挥作用。这是我在脚本中使用的内容,我在脚本中搜索每个 EmployeeID 的最新合同结束日期:
非常感谢!
I must say that the optimized version is one sweet, elegant piece of code, and it works like a charm even when the comparison is performed on a DATETIME column. This is what I used in my script, where I was searching for the latest contract end date for each EmployeeID:
Many thanks!
我想基于表中的多个列删除重复记录,因此这种方法对我有用,
步骤 1 - 从重复记录中获取最大 id 或唯一 id
步骤 2 - 从表中获取单个记录的 id
步骤3 - 从删除到
最终查询中排除上述 2 个查询:-
通过此查询,仅删除重复的记录。
I want to remove duplicate records based on multiple columns in table, so this approach worked for me,
Step 1 - Get max id or unique id from duplocate records
Step 2 - Get ids of single records from table
Step 3 - Exclude above 2 queries from delete to
Final Query :-
By this query only duplocate records will delete.
我创建的很好的存储过程,用于删除表的所有重复记录,而不需要该表上现有的唯一 ID。
Nice stored procedure I created for deleting all duplicate records of a table without needing an existing unique id on that table.
请尝试以下解决方案(基于“@Jose Rui Santos”答案的评论):
Please try the following solution (based on the comments of the '@Jose Rui Santos' answer):
这是经过尝试和测试的确切方法
This is the exact way Tried and Tested