在mysql中是否有相当于Oracle的rowid?
delete from my_table where rowid not in (select max(rowid) from my_table group by field1,field2)
我想做一个MySQL等效于此查询!!!
我想做的是::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
由于这是一次操作,因此不会带来太多开销。