Equivalent of Oracle’s RowID in MySQL
Solution 1
In MySql you usually use session variables to achive the functionality:
SELECT @rowid:[email protected]+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.
Solution 2
Maybe, I am misreading the question but your query (even in Oracle) doesn't accomplish your desired goal:
delete from my_table where rowid not in (select max(rowid) from
my_table group by field1,field2)
MySQL equivalent is
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.
Solution 3
mysql> set @row_num = 0;
First set rowId or row num the use it as following:
mysql> SELECT @row_num := @row_num + 1 as row_number,id,name,salary FROM employee
ORDER BY salary;
Related videos on Youtube

raj
#include<string.h> #define St_TGI7 * #include<stdio.h> int a;int main(){ #define CH_T char #define pT printf CH_T St_TGI7 p = " CL)\"EHSH)\"1*" "/T-*/$; $/$-*/$" "=\"/$C& ?\"-$A:" "-\"-$C* ;\"-$-*" "/$?\"-$ -*/\"A" "\"-$-*; (/\"=*;" "(/\"=*Q \"=*/\"" "_*/$ 5$ 5l -X)," "-V/&/VCX?^9f-*"; #define RAJ "%c" #define In_5_ int int G5Y=0,G62T=8; for ( int i = 0 ; i < strlen(p);i++ ) {In_5_ C =p[i]; In_5_ T ;if(C==32 ) continue ;C-=32 ; In_5_ A= C & 1; A += 32 ; G5Y=G5Y ;C >>= 1; while ( C--) { pT (RAJ,A) ; G5Y++;if(G5Y%44 ==0) pT (RAJ,10); }}} CH_T GAH876_;
Updated on April 20, 2022Comments
-
raj 29 days
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)..!!
-
Jerry Saravia about 10 yearsI think you misread the query. It will only delete duplicates.
-
Caius Jard about 1 yearNote: the OP asked for ROWID, and you gave them ROWNUM - they're completely different things in Oracle-world