How to get the article M in the table to nth record (n greater than M) for the three major databases

Source: Internet
Author: User

Database fetch table M to nth record (n>m)

1. Oracle Database: (Note: Tablename.id refers to TableName's primary key)

SELECT * FROM

(select Tablename.*,rownum as con from tableName

where RowNum <= m

ORDER BY tablename.id Desc)

where con >= n;


2. SQL Server database: (Note: Tablename.id refers to the primary key of the TableName)

Realization Principle Explanation:

1) first to detect the first M noodle Records

2) re-detect the top n Noodle Records

3) Finally filter out the previous m record by condition

Method One:

Select Top N-m+1 * from TableName

where Tablename.id not in

(select top m tablename.id from TableName ORDER by Desc Tablename.id)

ORDER BY Tablename.id

Method Two:

Select Top N-m+1 * from TableName as a where not exists

(SELECT * FROM

(select top M * from TableName ORDER BY id DESC) b

where b.id = a.id)

ORDER BY id DESC


3. mysql Database:

SELECT * from TableName limit m, N;


This article is from the "focus on Development Technology" blog, please be sure to keep this source http://bingge2015.blog.51cto.com/4332222/1847492

How to get the article M in the table to nth record (n greater than M) for the three major databases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.