Two methods for parsing database pagination compare of row_number of over of and top


Today, the teacher took us to review 1 database pagination, in general, today feel good, because the previous learning has not forgotten. All right, to get to the point, First, let’s talk about the top method The top method is essentially taking the number of pages that you want to look up and taking the number of pages that you want to look up Ex. :

 1 page 3 The data   Take the first 1 Pages of data
--  The first 1 page
       select top 3 * from T_news;
                        Take the first 5 Pages of data
-- The first 5 page
       select  top 3 * from T_News where id not in (select top (3*4) id from T_News)      -- The point is not  in on   It's up to him to get rid of the first few pages
                     If you want to set the number of data per page and the number of pages you look at, that's fine   Just add another stored procedure
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 So this is essentially adding another column to the table to determine what the number is Ex. :

                       1 page 3 The data   Take the first 1 Pages of data
   select * from (select *,ROW_NUMBER()over(order by id asc) as number from T_News ) as tb1
     where number between 1 and 3;
 The first 5 Pages of data
 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 number of data per page and the number of pages you look at
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

That’s it. That’s my understanding. I hope I can help you