与MySQL中的Oracle RowID等效 [英] Equivalent of Oracle’s RowID in MySQL

查看:270
本文介绍了与MySQL中的Oracle RowID等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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)

我想使mysql等效于此查询!!!

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

我要执行的操作是:my_table没有主键..我正在尝试删除重复的值并强加一个主键(field1和field2的复合键)..

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)..!!

推荐答案

在MySql中,您通常使用会话变量来实现该功能:

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 :也就是说,您将需要创建一个临时表,将测距子查询插入到该临时表中,并通过与临时表联接而从原始表中删除(您将需要一些唯一的行标识符):

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.

这篇关于与MySQL中的Oracle RowID等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆