Mysql删除旧的重复项

时间:2013-07-12 10:32:28

标签: mysql database

我有这个数据的表格

id,archive id,ean,index,date,(...)

我有一些项目具有相同的存档ID,相同的ean,但索引不同。

所以在这种情况下,我想删除较旧的(基于日期)项,因此对于每个组合archive_id / index,结果将不会超过1个结果。

3 个答案:

答案 0 :(得分:2)

以下(未经测试)应该有效:

DELETE FROM someTable WHERE EXISTS ( SELECT id FROM someTable AS subqTable WHERE
subqTable.id = someTable.id
AND subqTable.ean = someTable.ean
-- and other equality comparisons
AND subqTable.date AFTER someTable.date)

答案 1 :(得分:0)

DELETE duplicates.*
FROM _table
JOIN _table AS duplicates
    ON (_table.archive_id = duplicates.archive_id AND _table.index = duplicates.index)
WHERE duplicates.date < _table.date;

答案 2 :(得分:0)

delete t1
from your_table t1
left join 
(
  select archive_id, ean, min(date) as mdate
  from your_table
  group by archive_id, ean
) t2 on t1.archive_id = t2.archive_id
     and t1.ean = t2.ean
     and t1.date = t2.mdate
where t2.mdate is null
相关问题