3 回答
TA贡献1836条经验 获得超13个赞
也许,我误解了问题,但是您的查询(即使在Oracle中)也无法实现您想要的目标:
delete from my_table where rowid not in (select max(rowid) from
my_table group by field1,field2)
相当于MySQL
SELECT @rowid:=max(rowid) from my_table;
DELETE FROM my_table where rowid != @rowid;
这将清除除最后一行以外的所有行。
要执行一次数据清理(删除重复记录),您可以执行以下操作:
CREATE TABLE my_table2 SELECT distinct f1, f2, f3, etc from my_table;
DROP TABLE my_table;
ALTER TABLE my_table2 RENAME my_table;
然后通过ALTER TABLE添加任何必要的列和键。上面的代码可能需要删除您可能拥有的任何外键。
TA贡献2037条经验 获得超6个赞
您可以使用另一个派生表来避免临时表:
DELETE FROM my_table USING my_table JOIN (
SELECT @rowid:=IF(@f1=field1 AND @f2=field2, @rowid+1, 0) as rowid,
@f1:=field1 as field1,
@f2:=field2 as field2,
some_row_uid
FROM testruns t, (SELECT @rowid:=NULL, @f1:=NULL, @f2:=NULL) as init
ORDER BY field1, field2 DESC) as duplicates
ON my_table.some_row_uid = duplicates.some_row_uid AND duplicates.rowid > 0
添加回答
举报