删除重复记录4种方法
1.DELETE FROM tab1 a
WHERE rowid < ( SELECT MAX(b.rowid)
FROM tab1 b
WHERE b.name = a.name
AND b.price = a.price );
2.DELETE FROM tab1 a
WHERE EXISTS ( SELECT *
FROM tab1 b
WHERE b.name = a.name
AND b.price = a.price
AND a.rowid < b.rowid );
3.DELETE FROM tab1 a
WHERE rowid IN ( SELECT rowid
FROM tab1
EXCEPT
SELECT MAX(rowid)
FROM tab1
GROUP BY name, price );
4.DELETE FROM tab1 a
WHERE rowid NOT IN ( SELECT MAX(rowid)
FROM tab1
GROUP BY name, price );
第1和第2种方法关联子查询的性能问题是难点。
第3和第4种方法的性能优劣取决于表的规模,以及删除行数与留下的行数之间的比率。