在Mysql中,要想删除一个表中重复的数据,并且只保留一条,该如何做?
例如,库中存在表t_invest_return_record_sum (t),
有字段id,partner_id,invest_return_time,product_id ,有如下几条记录
(1,‘b’,'c','d'),(2,‘b’,'c','d'),(5,‘b1’,'c1','d1')
现要根据b,c,d三个字段重复的删除,留下id最小的一条(最多只有两条重复)
DELETE ta FROM t as ta INNER JOIN (SELECT max(tt.id) id FROM t as tt
GROUP BY tt.b,tt.c,tt.d HAVING COUNT(1)>=2) tb ON ta.id = tb.id;不能直接删除,如
DELETE FROM t where id in (SELECT id FROM t as tt
GROUP BY tt.b,tt.c,tt.d HAVING COUNT(1)>=2);如果重复数超过2个或者不确定重复数,则需要在子查询中处理一下
(1,‘b’,'c','d'),(2,‘b’,'c','d'),(3,‘b’,'c','d'),(4,‘b’,'c','d'),(5,‘b1’,'c1','d1')
保留id最小的那条
DELETE tt FROM t_invest_return_record_sum tt INNER JOIN
(SELECT a.id FROM t_invest_return_record_sum a INNER JOIN (SELECT t.partner_id,t.invest_return_time,t.product_id FROM t_invest_return_record_sum t GROUP BY t.partner_id,t.invest_return_time,t.product_id HAVING COUNT(1)>=2)t1 ON a.partner_id = t1.partner_id AND a.invest_return_time = t1.invest_return_time AND a.product_id = t1.product_id) t2 ON tt.id = t2.id LEFT JOIN (SELECT min(a.id) id FROM t_invest_return_record_sum a INNER JOIN (SELECT t.partner_id,t.invest_return_time,t.product_id FROM t_invest_return_record_sum t GROUP BY t.partner_id,t.invest_return_time,t.product_id HAVING COUNT(1)>=2)t1 ON a.partner_id = t1.partner_id AND a.invest_return_time = t1.invest_return_time AND a.product_id = t1.product_id) t3 ON tt.id = t3.id WHERE t3.id IS NULL