MySQL删除旧的重复项
问题描述:
我已经有了这张数据表
id,档案ID,ean,索引,日期,(...)
id, archive id, ean, index, date, (...)
我有一些项目具有相同的存档ID,相同的ean,但索引不同.
I've got some items with same archive id, same ean, but different index.
因此,在这种情况下,我想删除较旧的(基于日期)项目,因此结果将是,每个组合archive_id/index的结果都不会超过1个.
So in this case, I want to delete older (basing on date) item, so result will be that for each combination archive_id/index there will be no more than 1 result.
答
以下(未经测试)应该可以工作:
The following (untested) should work:
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)