Today, the teacher gave me a review of the pages in the database. In general, today is a good day, because I have not forgotten what I learned before. Now, go to the topic,
First, let's talk about the top method.
The top method is to remove the data before the data of the number of pages you want to query and then retrieve the first few
Example:
Copy codeThe Code is as follows:
Take the data on the first page
-- Page 1
Select top 3 * from T_news;
Take the data on the fifth page
-- Page 5
Select top 3 * from T_News where id not in (select top (3*4) id from T_News) -- the key is that not in is used to remove the data from the previous pages.
If you want to set the number of data entries on each page and view the number of pages, you can add more stored procedures.
Create proc usp_fenye @ geshu int, @ yeshu int
As
Begin
Select top (@ geshu) * from T_News where id not in (select top (@ geshu * (@ yeshu-1) id from T_News)
End
Then, let's talk about the ROW_NUMBER () over () method.
This is actually adding a column to the data table to determine the number of data entries.
Example:
Copy codeThe Code is as follows:
Take the data on the first page
Select * from (select *, ROW_NUMBER () over (order by id asc) as number from T_News) as tb1
Where number between 1 and 3;
Data on page 5
Select * from (select *, ROW_NUMBER () over (order by id asc) as number from T_News) as tb1
Where number between 3*4 + 1 and 3*5;
Set the data entries on each page and view the page number.
Create proc usp_fenye @ geshu int, @ yeshu int
As
Begin
Select * from (select *, ROW_NUMBER () over (order by id asc) as number from T_News) as tb1
Where number between @ geshu * (@ yeshu-1) + 1 and @ geshu * @ yeshu;
End
Well, that's what I understand. I hope it can help people who see it ~