SQL Server SQL语句查询分页数据的解决方案:

要求选取 tbllendlist 中 第3000页的记录,每一页100条记录

方法1:



​​view plain​​
​​copy to clipboard​​
​​print​​
​​?​​



1. select top 100 * from
2. where fldserialNo not in
3. (
4. select top 300100 fldserialNo from
5. order by
6. )
7. order by

select top 100 * from tbllendlist where fldserialNo not in ( select top 300100 fldserialNo from tbllendlist order by fldserialNo ) order by fldserialNo

方法2:


​​view plain​​
​​copy to clipboard​​
​​print​​
​​?​​



1. SELECT TOP
2. FROM
3. WHERE
4. (SELECT MAX(fldserialNo)
5. FROM (SELECT TOP
6. FROM
7. ORDER BY fldserialNo) AS
8. ORDER BY


SELECT TOP 100 * FROM tbllendlist WHERE (fldserialNo > (SELECT MAX(fldserialNo) FROM (SELECT TOP 300100 fldserialNo FROM tbllendlist ORDER BY fldserialNo) AS T)) ORDER BY fldserialNo

方法1执行速度比较快!

不过,这种做法还是很麻烦,强烈期待微软发明新的分页的SQL语句.

 
  SQL Server

从数据库表中的第M条记录开始取N条记录,利用Top关键字:注意如果Select语句中既有top,又有order by,则是从排序好的结果集中选择:
 SELECT *
   FROM  ( SELECT Top N *
         FROM    (SELECT Top (M + N - 1) * FROM 表名称 Order by 主键 desc) t1 ) t2
              Order by 主键 asc

 例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
     SELECT *
       FROM ( SELECT TOP 20 *
                           FROM (SELECT TOP 29 * FROM Sys_option order by sys_id desc) t1) t2
           Order by sys_id asc

 Oralce数据库

从数据库表中第M条记录开始检索N条记录
     SELECT *
           FROM (SELECT ROWNUM r,t1.* From 表名称 t1 where rownum < M + N) t2
           where t2.r >= M
     例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
      SELECT *
           FROM (SELECT ROWNUM R,t1.* From Sys_option where rownum < 30 ) t2
            Where t2.R >= 10
      
   My sql数据库

     My sql数据库最简单,是利用mysql的LIMIT函数,LIMIT [offset,] rows从数据库表中M条记录开始检索N条记录的语句为:
           SELECT * FROM 表名称 LIMIT M,N

            例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
            select * from sys_option limit 10,20