SQL statement -- Select data from the nth record to the nth record in the database

Source: Internet
Author: User

If there is a t_user data table in this way, the table ID increases automatically, but the IDs in the data table are not guaranteed to be continuous. Write an SQL statement, query the data between the n-th record and the M-th record (M> N> 0, all smaller than the total number of records in the data table)

 

The idea is as follows:

  1. First, find the ID of the nth record and the nth record.
  2. Obtain the ID of the nth record whose ID is greater than the nth Record ID and smaller than the nth Record ID.

CodeThe decomposition is as follows:

  1. Obtain the ID of the nth record:Select max (ID) from t_user where ID in (select Top n id from t_user)
     
  2. Obtain the ID of the MTH record:Select max (ID) from t_user where ID in (select top m id from t_user)
     

The SQL statement is as follows:

Select * From t_user where ID> (select max (ID) from t_user where ID in (select Top n id from t_user) and ID <(select max (ID) from t_user where ID in (select top m id from t_user ));

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.