尝试
DELETE d
FROM dummy1 d JOIN
(
SELECT d1.id, d1.name, MAX(d1.last_used_date) max_date
FROM dummy1 d1 JOIN dummy1 d2
ON d1.id = d2.id AND d1.name = d2.name
GROUP BY id, name
) q ON d.id = q.id AND d.name = q.name AND d.last_used_date <> q.max_date
这是SQLFiddle演示
更新要仅检查重复的 id,您可以稍微调整上面的查询
DELETE d
FROM dummy1 d JOIN
(
SELECT d1.id, MAX(d1.last_used_date) max_date
FROM dummy1 d1 JOIN dummy1 d2
ON d1.id = d2.id
GROUP BY id
) q ON d.id = q.id AND d.last_used_date <> q.max_date
这是SQLFiddle演示
UPDATE2删除具有最大时间戳的重复项
1)您可以通过引入带有IGNORE
选项的唯一索引。在这种情况下,MySql 将自行决定保留哪些记录。
ALTER TABLE dummy1 ENGINE MyISAM;
ALTER IGNORE TABLE dummy1 ADD UNIQUE (id, name);
ALTER TABLE dummy1 ENGINE InnoDB;