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

JS Object constructor Object.freeze

Table of contents Overview Example 1) Freeze Obje...

Solution to Vue3.0 error Cannot find module'worker_threads'

I'll record my first attempt at vue3.0. When ...

Common browser compatibility issues (summary)

Browser compatibility is nothing more than style ...

Summary of new usage of vi (vim) under Linux

I have used the vi editor for several years, but ...

Detailed examples of variable and function promotion in JavaScript

js execution Lexical analysis phase: includes thr...

vue+springboot realizes login verification code

This article example shares the specific code of ...

MySQL Full-text Indexing Guide

Full-text indexing requires special query syntax....

Two types of tab applications in web design

Nowadays, tabs are widely used in web design, but...

Detailed explanation of Docker usage under CentOS8

1. Installation of Docker under CentOS8 curl http...

Vue conditional rendering v-if and v-show

Table of contents 1. v-if 2. Use v-if on <temp...

CSS3 mobile vw+rem method to achieve responsive layout without relying on JS

1. Introduction (1) Introduction to vw/vh Before ...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...