我有这个数据的表格
id, 档案 id, ean, 索引, 日期, (...)
我有一些具有相同存档 ID、相同 ean 但索引不同的项目。
所以在这种情况下,我想删除较旧的(基于日期)项目,因此结果将是每个组合 archive_id/index 不会超过 1 个结果。
以下(未经测试)应该可以工作:
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)
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;
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