以下是您可能考虑的两种策略。您必须根据您实际考虑的重复情况来调整用于选择重复项的列。我只是列出了除id列之外的所有列出的列。
第一个简单地创建一个没有重复的新表。有时候这比尝试删除所有违规行更快更容易。只需创建一个新表,插入唯一的行(我使用min(id)作为结果行的id),重命名这两个表,并且(一旦您满意所有事情都可以正常工作),请删除原始表。当然,如果你有任何外键约束,你也必须处理这些。
create table table_copy like table_name;
insert into table_copy
(id, market, agent, report_name, producer_code, report_date, entered_date, sync)
select min(id), market, agent, report_name, producer_code, report_date,
entered_date, sync
from table_name
group by market, agent, report_name, producer_code, report_date,
entered_date, sync;
RENAME TABLE table_name TO table_old, table_copy TO table_name;
drop table table_old;
第二个策略,刚刚删除重复项,使用临时表来保存哪些行有重复,因为MySQL不会允许您从您从在子查询中删除同一表中选择信息。只需创建一个临时表,其中包含标识重复项的列以及实际上将保留该id的id列,然后您可以执行多表删除,您可以在其中加入两个表以选择重复项。
create temporary table dups
select min(id), market, agent, report_name, producer_code, report_date,
entered_date, sync
from table_name
group by market, agent, report_name, producer_code, report_date,
entered_date, sync
having count(*) > 1;
delete t
from table_name t, dups d
where t.id != d.id
and t.market = d.market
and t.agent = d.agent
and t.report_name = d.report_name
and t.producer_code = d.producer_code
and t.report_date = d.report_date
and t.entered_date = d.entered_date
and t.sync = d.sync;
太好了,刚刚尝试过你的第一个方法,它似乎已经完美工作,只需要大约30分钟执行。谢啦。 – Skippy 2011-03-17 07:48:27
只需要在另一个数据库上做同样的事情,第二种方法也可以完美地工作。再次感谢。 – Skippy 2011-08-05 07:16:09