有选择地从MySQL删除大部分重复的记录 [英] Selectively delete mostly duplicate records from MySQL

查看:66
本文介绍了有选择地从MySQL删除大部分重复的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子:

PRICE_UPDATE
id (int 5, auto-increment, primary, unique)
part_number (varchar 10, non-null)
price (float(10,2), non-null)

某些part_number已重复(1条或更多重复记录).有时价格相同,有时价格不同.

Some of the part_numbers are duplicated (1 or more duplicate records). Sometimes with the same price, sometimes with different prices.

如何根据part_number删除所有重复的行,如果价格相同,则保留最高价格或仅保留1条记录?

这甚至可以在直接的MySQL中实现吗?

Is this even doable in straight MySQL?

推荐答案

DELETE t1
    FROM YourTable t1, YourTable t2
    WHERE t1.part_number = t2.part_number
        AND (t1.price, t1.id) < (t2.price, t2.id)

这篇关于有选择地从MySQL删除大部分重复的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆