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)..!!
First set rowId or row num the use it as following:
In MySql you usually use session variables to achive the functionality:
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):
Since that is one time operation, this should not bring too much overhead.
you can avoid the temp table using another derived table:
Maybe, I am misreading the question but your query (even in Oracle) doesn't accomplish your desired goal:
MySQL equivalent is
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:
Then add whatever columns & keys necessary by ALTER TABLE. Above code might require to drop any foreign keys you might have.