rowid 对应mysql,在MySQL中相当于Oracle的RowID

is there an equivalent of oracle's rowid in mysql?

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

I want to make a mysql equivalent of this query!!!

What i'm trying to do is, : The my_table has no primary key.. i'm trying to delete the duplicate values and impose a primary key (composite of field1, field2)..!!

解决方案

In MySql you usually use session variables to achive the functionality:

SELECT @rowid:=@rowid+1 as rowid

FROM table1, (SELECT @rowid:=0) as init

ORDER BY sorter_field

But you can not make sorts on the table you are trying to delete from in subqueries.

UPD: that is you will need to create a temp table, insert the ranging subquery to the temp table and delete from the original table by joining with the temporary table (you will need some unique row identifier):

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

Since that is one time operation, this should not bring too much overhead.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值