小弟我刚接触数据库,对分页语句中实现多主键表中分页查询不是很清楚望大家指点~
举例说明如:
       教师表
教师编号(主) 班级编号(主) 教师姓名 班级人数1              1              a    201              2              a    231              3              a    202              1              b    202              3              b    203              2              c    233              3              c    20在表中我想实现按每页两条记录展现表中全部内容应该怎么写!

解决方案 »

  1.   

    declare @str varchar(1000)
    declare @i int --第几页
    select @i=1
    select @str='select top 2 * from 教师表 where convert(varchar(10),教师编号)+convert(varchar(10),班级编号)
    not in (select top '+convert(varchar(10),2*(@i-1))+' [id]=convert(varchar(10),教师编号)+convert(varchar(10),班级编号) from 教师表 order by 教师编号,班级编号) order by 教师编号,班级编号'
    exec(@str)
      

  2.   

    测试
    --------
    create table 教师表 (教师编号 int, 班级编号 int , 教师姓名 varchar(2),班级人数 int )insert into 教师表 
    select 1    ,   1     ,   'a'  , 20
    union all
    select  1        ,      2      ,       'a'   , 23 
    union all
    select 1      ,        3      ,        'a',    20 
    union all
    select 2     ,        1      ,        'b',    20 
    union all
    select 2      ,        3      ,        'b',    20
    union all
    select 3       ,       2      ,        'c',    23
    union all 
    select 3       ,       3      ,        'c' ,  20 查询第 3 页
    ---------declare @str varchar(1000)
    declare @i int --第几页
    select @i=3
    select @str='select top 2 * from 教师表 where convert(varchar(10),教师编号)+convert(varchar(10),班级编号)
    not in (select top '+convert(varchar(10),2*(@i-1))+' [id]=convert(varchar(10),教师编号)+convert(varchar(10),班级编号) from 教师表 order by 教师编号,班级编号) order by 教师编号,班级编号'
    exec(@str)查询结果
    ------------教师编号        班级编号        教师姓名 班级人数        
    ----------- ----------- ---- ----------- 
    2           3           b    20
    3           2           c    23
      

  3.   

    额。。比单主键的复杂好多。手边电脑没装SQL无法测试,不过先谢谢了~