access数据库取中间某段记录

m为上标,n为下标,例如取出第8到12条记录,m=8,n=12,Table为表名
Select Top n-m+1 * From Table  Where Id>(Select Max(Id) From  (Select Top m-1 Id From Table order By Id Asc) Temp)  order By Id Asc 

从TABLE表中取出第m到n条记录 (Exists版本)
Select TOP n-m+1 * FROM TABLE AS a Where Not Exists(Select * From (Select Top m-1 * From TABLE order by id) b Where b.id=a.id ) order by id

select * from table_name where id_field not in (select top 3 id_field from table_name)

 

 

注意,其实就是把 select top 语句写两遍,第二遍包含了第一遍,然后把是第一遍中的记录用not in剔除就可以了,所以两个where都必须包含相同的外加条件。

asp 中经常用来写 pageno 的程序,因为直接用ado的pagesize如果遇上几十万数据,用pagesize就完蛋喽。

关于 not in 语句速度比较慢,某人提示将not in改为

select table2.* from table2 left join table1 on table2.key=table1.key where (able1.key is null)

 

返回第3条记录用:

select top 1 * from (Select top 3 a.编号 FROM a order by 编号) as b order by 编号 desc

 

如果用来进行分页,返回第100-150条记录,可以用

select top 50 * from (Select top 150 a.编号 FROM a order by 编号) as b order by 编号 desc

 

 

以下是 SQL SERVER 2000 T-SQL 分页代码

--------------------------
-- 分页代码

 

IF EXISTS (Select name 
       FROM   sysobjects 
       Where  name = N'p_GetTopic' 
       AND       type = 'P')
    Drop PROCEDURE p_GetTopic
GO

Create PROCEDURE p_GetTopic 
    @PageSize    int =15,
    @CurPage    int =1,
    @OrderBy    varchar(50) = 'desc'
    
AS
    declare @sql varchar(4000)
    --declare @intCount int
    --select @intCount = count(*) from t_Issue
    
    if LOWER(@OrderBy) = 'asc'
    begin
        select @sql=    'select ti.*,
                    tu.username,
                    tu.usernickname,
                    tu.ranknum,
                    tu.credit,
                    tr.roomname
                from    (select top ' + cast(@PageSize as varchar(20)) + ' * from t_issue 
                    where topicid > isnull((select max(topicid) from (select top ' + cast((@PageSize * (@CurPage -1) ) as varchar(20)) + ' * from t_Issue order by topicid ) a),0)  
                    order by topicid) ti
                left join t_user tu on ti.postuserid = tu.userid 
                left join t_room tr on ti.roomid = tr.roomid
                order by ti.topicid
                '
    end
    else
    begin
        select @sql=    'select ti.*,
                    tu.username,
                    tu.usernickname,
                    tu.ranknum,
                    tu.credit,
                    tr.roomname
                from    (select top ' + cast(@PageSize as varchar(20)) + ' * from t_issue 
                    where topicid < isnull((select min(topicid) from (select top ' + cast((@PageSize * (@CurPage -1) ) as varchar(20)) + ' * from t_Issue order by topicid desc) a),99999999)  
                    order by topicid desc) ti
                left join t_user tu on ti.postuserid = tu.userid 
                left join t_room tr on ti.roomid = tr.roomid
                order by ti.topicid desc
                '
        
    end
    
    execute(@sql)
    
    select count(*) as RecordCount from t_issue
GO

-- =============================================
-- example to execute the store procedure
-- =============================================
EXECUTE p_GetTopic 233,1
GO

 

版权声明:
作者:Kiyo
链接:https://www.wkiyo.cn/html/2009-08/i602.html
来源:Kiyo's space
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>