Home > Database > Mysql Tutorial > What are the MySQL Alternatives to Oracle's RowID?

What are the MySQL Alternatives to Oracle's RowID?

Linda Hamilton
Release: 2024-12-05 00:34:11
Original
900 people have browsed it

What are the MySQL Alternatives to Oracle's RowID?

MySQL Equivalent of Oracle's RowID

In MySQL, there is no direct equivalent to Oracle's RowID. However, there are several ways to achieve similar functionality.

One approach is to use session variables and subqueries:

SELECT @rowid:=@rowid+1 as rowid
FROM table1, (SELECT @rowid:=0) as init
ORDER BY sorter_field
Copy after login

This will assign a unique identifier to each row, which can be used for sorting or other purposes.

Another approach to deleting duplicate records is to create a temporary table and perform a join between the temporary table and the original table:

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
Copy after login

This approach is recommended when sorting cannot be performed on the table being deleted from in subqueries.

The above is the detailed content of What are the MySQL Alternatives to Oracle's RowID?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template