小弟我的sql语句这样写的:
 sql = "select top " + secondly + " * from " + table + " where " + id + " not in(select top " + first + " " + id + " from " + table + ")";
secondly :设定的翻页大小,
table :查询的表名,
id :主键
first 翻页大小*(数据总数-1)
这是控制翻页的,
但现在我要将翻页中去除重复的id数据,并是最新的数据显示。
我不知道怎么写了,还请各位达人帮帮忙!~~

解决方案 »

  1.   


    --这样子翻页来查询如果id唯一是主键,而且只有一个表,翻页是正确的,如果是多表或者id是可以重复的
    --那么翻页后可能会丢失id重复的数据!
    --下边的翻页应该可以满足下你的需求,不是很完善!-- 2005 翻页!
    USE [model]
    GO
    /****** 对象:  StoredProcedure [dbo].[getpate_2005]    脚本日期: 01/19/2011 19:17:48 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO-- ===================================================
    -- auhtor : acherat
    -- modified date : 01/10/2011 20:41:44
    -- description : 分页存储
    -- ===================================================ALTER proc [dbo].[getpate_2005]
    (
    @tbname nvarchar(4000), --表名及多表连接语句
    @fieldcol nvarchar(4000), --查询的字段名
    @pagesize int, --分页尺寸
    @pagecurrent int, --需找寻的页码
    @strwhere nvarchar(4000) = null, --表查询的限制条件
    @strorder nvarchar(4000) = null, --表查询的排序
    @pagecount int output --返回总页数
    )
    as
    begin
    set nocount on
    declare @strsql nvarchar(4000) --返回结果集动态SQL语句
    declare @strtmp nvarchar(4000) --计算总页数动态SQL语句
    declare @colshow nvarchar(4000)
    declare @counts int--参数初始化
    if (@fieldcol is null or @fieldcol = '')
    set @fieldcol = '*'
    if (isnull(@pagesize,0) < 1)
    set @pagesize = 10
    if (isnull(@pagecurrent,0) < 1)
    set @pagecurrent = 1
    if (@strwhere is null or @strwhere = '')
    set @strwhere = ' '
    else
    set @strwhere = N' where ' + @strwhere
    if (@strorder is null or @strorder = '')
    set @strorder = ' '
    else
    set @strorder = N' order by ' + @strorder--总页数设置
    if @pagecount is null
    begin
    set @strtmp = N' select @counts = count(*) from ' + @tbname + @strwhere + @strorder
    exec sp_executesql @strtmp,N'@counts int output',@counts output
    set @pagecount = (@counts + @pagesize - 1)/@pagesize
    end--查询结果设置
    if (@pagecount >= 1)
    begin
    set @colshow = (select dbo.f_strcol(@fieldcol))
    set @strsql = N'select ' + @colshow + N' from (select row_number() over (order by getdate())iii,' + @fieldcol
    +N' from ' + @tbname + @strwhere + @strorder +N')temp where iii between ' + ltrim((@pagecurrent - 1)*@pagesize + 1)
    +N' and ' + ltrim(@pagecurrent*@pagesize)
    end
    exec(@strsql)
    set nocount off
    end
    --辅助函数!
    USE [model]
    GO
    /****** 对象:  UserDefinedFunction [dbo].[f_strcol]    脚本日期: 01/19/2011 19:18:02 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER function [dbo].[f_strcol](@str nvarchar(4000))
    returns nvarchar(4000)
    as
    begin
    declare @strsg nvarchar(4000)
    declare @strbg nvarchar(4000)
    declare @stras nvarchar(4000)
    declare @len int
    declare @dou int
    declare @dian int
    set @strsg = @str + ','
    set @strbg = ''
    set @len = len(@strsg)
    set @dou = charindex(',',@strsg)
    set @dian = charindex('.',@strsg)
    while (@dou > 0)
    begin
    set @stras = substring(@strsg,@dian+1,@dou-@dian-1)
    if charindex('as',@stras) > 0
    set @stras = ltrim(rtrim(right(@stras,len(@stras)-charindex('as',@stras)-1)))
    set @strbg = @strbg + ',' + @stras
    set @strsg = substring(@strsg,@dou+1,@len-@dou)
    set @dou = charindex(',',@strsg)
    set @dian = charindex('.',@strsg)
    set @len = len(@strsg)
    end
    return stuff(@strbg,1,1,'')
    end
      

  2.   

    sql = "select top " + secondly + " * from (select row_number() over (order by getdate())rn,* from " + table + ") where rn >= " + first;
      

  3.   


    谢谢大家@!!~~~~(>_<)~~~~ 
      

  4.   

    我刚才以为陈旭远是你的名字呢
    你的用户名:chenyx被我看成(chenxy)了
    程序员是cheng呀
      

  5.   

    去除重复的id数据?id是主键,应该是唯一的。
    如果id不唯一。就重新排序生成一个唯一id用了分页。