相当于 MySQL 中 Oracle 的 RowID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2728413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of Oracle’s RowID in MySQL
提问by raj
is there an equivalent of oracle's rowid in mysql?
mysql 中是否有相当于 oracle 的 rowid?
delete from my_table where rowid not in (select max(rowid) from my_table group by field1,field2)
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!!!
我想制作一个相当于这个查询的 mysql !!!
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)..!!
我想要做的是: my_table 没有主键.. 我正在尝试删除重复值并强加一个主键(field1、field2 的组合)..!!
回答by newtover
In MySql you usually use session variables to achive the functionality:
在 MySql 中,您通常使用会话变量来实现功能:
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):
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
Since that is one time operation, this should not bring too much overhead.
由于这是一次操作,因此不应带来太多开销。
回答by Alexey Gerasimov
Maybe, I am misreading the question but your query (even in Oracle) doesn't accomplish your desired goal:
也许,我误读了这个问题,但您的查询(即使在 Oracle 中)也没有实现您想要的目标:
delete from my_table where rowid not in (select max(rowid) from
my_table group by field1,field2)
MySQL equivalent is
MySQL等价物是
SELECT @rowid:=max(rowid) from my_table;
DELETE FROM my_table where rowid != @rowid;
This will wipe out all rows except for last one.
这将清除除最后一行之外的所有行。
To perform one time cleanup (removing duplicate records) of your data you can do this:
要对数据执行一次性清理(删除重复记录),您可以执行以下操作:
CREATE TABLE my_table2 SELECT distinct f1, f2, f3, etc from my_table;
DROP TABLE my_table;
ALTER TABLE my_table2 RENAME my_table;
Then add whatever columns & keys necessary by ALTER TABLE. Above code might require to drop any foreign keys you might have.
然后添加 ALTER TABLE 所需的任何列和键。上面的代码可能需要删除您可能拥有的任何外键。
回答by Mukesh Kumar
mysql> set @row_num = 0;
First set rowId or row num the use it as following:
首先设置 rowId 或 row num 使用它如下:
mysql> SELECT @row_num := @row_num + 1 as row_number,id,name,salary FROM employee
ORDER BY salary;
回答by mosheb
you can avoid the temp table using another derived 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