当前位置: 代码迷 >> Sql Server >> 请问关于SQL语句分页的有关问题
  详细解决方案

请问关于SQL语句分页的有关问题

热度:63   发布时间:2016-04-24 19:35:02.0
请教关于SQL语句分页的问题
以下是查询第二页的数据:
select distinct Top 10 a.id,a.DonforUserGsm,a.Name,a.DonforUserZycp,a.DonforUserDh,a.DonforUserYhjf,a.DonforUserSzs,a.DonforUserDjs,a.DonforUserVip,a.DonforUserQq,a.DonforUserVip,a.DonforUserYhjf,a.DonforUserJcSj from [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 and (a.id Not in (Select Top 20 a.id From [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc)) Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc

以下是查询第三页的数据:
select distinct Top 10 a.id,a.DonforUserGsm,a.Name,a.DonforUserZycp,a.DonforUserDh,a.DonforUserYhjf,a.DonforUserSzs,a.DonforUserDjs,a.DonforUserVip,a.DonforUserQq,a.DonforUserVip,a.DonforUserYhjf,a.DonforUserJcSj from [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 and (a.id Not in (Select Top 30 a.id From [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc)) Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc

每页显示10条

现在按上面的操作第二页会重复第一页的数据,第三页会重复第二页的数据······

我知道是中间“a.id not in”后面的查询语句没加“distinct”,但是加上这个又出现ORDER BY 后台的数据必须要在查询语句,如果查询语句加上这些参数,a.id又无法比较,头痛啊

请教各位如何解决这个问题好,谢谢

------解决方案--------------------
引用:
以下是查询第二页的数据:
select distinct Top 10 a.id,a.DonforUserGsm,a.Name,a.DonforUserZycp,a.DonforUserDh,a.DonforUserYhjf,a.DonforUserSzs,a.DonforUserDjs,a.DonforUserVip,a.DonforUserQq,a.DonforUserVip,a.DonforUserYhjf,a.DonforUserJcSj from [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 and (a.id Not in (Select Top 20 a.id From [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc)) Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc

以下是查询第三页的数据:
select distinct Top 10 a.id,a.DonforUserGsm,a.Name,a.DonforUserZycp,a.DonforUserDh,a.DonforUserYhjf,a.DonforUserSzs,a.DonforUserDjs,a.DonforUserVip,a.DonforUserQq,a.DonforUserVip,a.DonforUserYhjf,a.DonforUserJcSj from [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 and (a.id Not in (Select Top 30 a.id From [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc)) Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc

每页显示10条

现在按上面的操作第二页会重复第一页的数据,第三页会重复第二页的数据······

我知道是中间“a.id not in”后面的查询语句没加“distinct”,但是加上这个又出现ORDER BY 后台的数据必须要在查询语句,如果查询语句加上这些参数,a.id又无法比较,头痛啊

请教各位如何解决这个问题好,谢谢



价格rownumber() over() 行号
然后用这个进行分页。
你那种分页有重复数据,分页条件不能用带重复数据的字段。
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

以下是查询第二页的数据:
select distinct Top 10 a.id,a.DonforUserGsm,a.Name,a.DonforUserZycp,a.DonforUserDh,a.DonforUserYhjf,a.DonforUserSzs,a.DonforUserDjs,a.DonforUserVip,a.DonforUserQq,a.DonforUserVip,a.DonforUserYhjf,a.DonforUserJcSj from [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 and (a.id Not in (Select Top 20 a.id From [user] a,Donfor_Xxfbb_Lb b,Donfor_Xxfbb_Lb c Where CharIndex(b.Lbmc,a.DonforUserZycp)>0 and b.Ssid=c.id and c.id=18 Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc)) Order By a.DonforUserVip Desc,a.DonforUserYhjf Desc,a.DonforUserJcSj Desc
  相关解决方案