MySQL中的Oracle的RowID相当于

有没有相当于oracle的rowid在MySQL?

delete from my_table where rowid not in (select max(rowid) from my_table group by field1,field2)

我想让这个查询的MySQL相当于!

我想要做的是,:my_table没有主键..我试图删除重复的值,并强加一个主键(复合field1,field2)..!

在MySql中,通常使用会话variables来实现function:

 SELECT @rowid:=@rowid+1 as rowid FROM table1, (SELECT @rowid:=0) as init ORDER BY sorter_field 

但是你不能在你试图从子查询中删除的表上进行sorting。

UPD :这就是你将需要创build一个临时表,插入测距子查询到临时表,并删除从原始表join临时表(您将需要一些唯一的行标识符):

 CREATE TEMPORARY TABLE duplicates ... INSERT INTO duplicates (rowid, field1, field2, some_row_uid) 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; DELETE FROM my_table USING my_table JOIN duplicates ON my_table.some_row_uid = duplicates.some_row_uid AND duplicates.rowid > 0 

既然这是一次性操作,这不应该带来太多的开销。

也许,我误解了这个问题,但是你的查询(甚至在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 FROP my_table where rowid != @rowid; 

这将清除除最后一行以外的所有行。

要执行一次清理(删除重复logging)的数据,你可以这样做:

 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所需的任何列和键。 以上代码可能需要删除您可能拥有的任何外键。

您可以使用另一个派生表来避免临时表:

 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