Detailed example of mysql similar to oracle rownum writing

Detailed example of mysql similar to oracle rownum writing

Rownum is a unique way of writing in Oracle. In Oracle, rownum can be used to retrieve the first piece of data or to limit the number of batches to be written when writing data in batches.

How to write the first data in mysql

SELECT * FROM t order by id LIMIT 1;

How to write the first data in oracle

SELECT * FROM t where rownum =1 order by id;

OK, the above is a comparison of the writing methods of MySQL and Oracle to get the first data, but this is only one usage of rownum. Rownum can also be used to write data in batches.

Write 10,000 records to table t in batches:

 insert into t(id,date) select sys_guid(),sysdate from dual connect by rownum<=10000;

Oracle original writing:

select * from (select id,name from t) where rownum <![CDATA[<=]]> to_number(num);

SQL rewritten by mysql:

SELECT 
 * 
FROM
 (SELECT 
  tb.*,
  @rownum := @rownum + 1 AS rownum 
 FROM
  (SELECT 
   id,
   NAME 
  FROM
   t) tb,
  (SELECT 
   @rownum := 0) r) AS t 
WHERE rownum <= CAST(num AS SIGNED INTEGER);

The above is all the knowledge points introduced this time. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the misunderstanding between MySQL and Oracle
  • Implementation of SpringBoot multi-database connection (mysql+oracle)
  • Detailed explanation of the solution for real-time synchronization from MySQL to Oracle
  • Example of creating table statements for user Scott in MySQL version of Oracle
  • Description of the default transaction isolation level of mysql and oracle
  • Description of the correspondence between MyBatis JdbcType and Oracle and MySql data types
  • Summary of the differences between MySQL and Oracle (comparison of functional performance, selection, SQL when using them, etc.)
  • A brief discussion on the differences between the three major databases: Mysql, SqlServer, and Oracle
  • Problems and solutions when replacing Oracle with MySQL

<<:  jQuery implements employee management registration page

>>:  How to install Linux system (Redhat8) and virtual machine network configuration in VMware

Recommend

Summary of commonly used tags in HTML (must read)

Content Detail Tags: <h1>~<h6>Title T...

Detailed explanation of CSS3 to achieve responsive accordion effect

I recently watched a video of a foreign guy using...

Two ways to clear table data in MySQL and their differences

There are two ways to delete data in MySQL: Trunc...

Discussion on CSS style priority and cascading order

In general : [1 important flag] > [4 special fl...

Installation, configuration and use of process daemon supervisor in Linux

Supervisor is a very good daemon management tool....

Ubuntu 20.04 Chinese input method installation steps

This article installs Google Input Method. In fac...

Detailed explanation of the use of filter properties in CSS

The filter attribute defines the visual effect of...

Beginners learn some HTML tags (3)

Beginners who are exposed to HTML learn some HTML...

MYSQL subquery and nested query optimization example analysis

Check the top 100 highest scores in game history ...

Detailed explanation of the role of brackets in AngularJS

1. The role of brackets 1.1 Square brackets [ ] W...

Detailed explanation of Docker common commands Study03

Table of contents 1. Help Command 2. Mirror comma...

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

A brief introduction to web2.0 products and functions

<br />What is web2.0? Web2.0 includes those ...

VUE implements token login verification

This article example shares the specific code of ...