在
mysql中有相当于oracle的rowid吗?
从my_table中删除rowid不在(从my_table组中的max(rowid)by field1,field2)
我想要做的是:my_table没有主键..我想删除重复的值并强加一个主键(field1,field2的组合).. !!
在
MySql中,通常使用会话变量来实现功能:
SELECT @rowid:=@rowid+1 as rowid FROM table1,(SELECT @rowid:=0) as init ORDER BY sorter_field
UPD:那就是您需要创建一个临时表,将测距子查询插入临时表,并通过加入临时表(原始表中删除)删除原始表(您将需要一些唯一的行标识符):
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
既然这是一次性的操作,这不应该带来太多的开销.