【问题标题】:How to get result count while paginating the SQL Server query如何在分页 SQL Server 查询时获取结果计数
【发布时间】:2012-11-30 12:25:58
【问题描述】:

为了显示总页码计数,我还需要在以下查询中检索结果总数。我该怎么做?

谢谢

select 
    AvgLevel, TotalCount, PokemonId, Type1, Speed, MonsterTotalStats 
from 
   (select 
       row_number() over (order by tblPokedex.PokemonId asc) rowNumber,
       AvgLevel, TotalCount, tblPokedex.PokemonId, 
       Type1, tblPokedex.Speed, MonsterTotalStats
    from 
       tblPokemonStats, tblAvailablePokemons, tblPokedex 
    left join 
       tblUsersPokemons on tblPokedex.PokemonId = tblUsersPokemons.PokemonId  
    where 
       tblPokemonStats.PokemonId = tblPokedex.PokemonId 
       and tblPokedex.Class = 'emissary' 
    group by 
       tblPokedex.PokemonId, tblPokedex.Type1, tblPokedex.Speed, 
       tblPokemonStats.AvgLevel, tblPokemonStats.TotalCount, MonsterTotalStats 
   ) result 
where 
    result.rowNumber > 0 and result.rowNumber < 101

【问题讨论】:

  • 感谢编辑 @marc_s 现在看起来好多了

标签: sql pagination sql-server-2008-r2 rowcount


【解决方案1】:

只需添加列Count(*) over()

..... 
select row_number() 
over (order by tblPokedex.PokemonId asc) rowNumber,
AvgLevel,
Count(*) over() as TotalCount,
.......

查看示例

select 
AvgLevel,TotalCount,PokemonId,Type1,Speed,MonsterTotalStats, TOTALRECORDCOUNT
from (
select row_number() 
over (order by tblPokedex.PokemonId asc) rowNumber,AvgLevel,TotalCount,tblPokedex.PokemonId,Type1,tblPokedex.Speed,MonsterTotalStats,
count(*) over() as TOTALRECORDCOUNT
from tblPokemonStats,tblAvailablePokemons,tblPokedex left join tblUsersPokemons on tblPokedex.PokemonId=tblUsersPokemons.PokemonId  
where tblPokemonStats.PokemonId=tblPokedex.PokemonId and tblPokedex.Class='emissary' 
group by tblPokedex.PokemonId,tblPokedex.Type1,tblPokedex.Speed,tblPokemonStats.AvgLevel,tblPokemonStats.TotalCount,MonsterTotalStats 
)  
result where result.rowNumber>0 and result.rowNumber<101

【讨论】:

  • TotalCount 是一个存在于另一个表格中的参数,显示玩家捕获了多少怪物。
  • 好的,可以称之为“TotalRecordCount”,因此您从该查询获得的每条记录中都会包含总记录数。
  • 哦,我的错。我看错了。但它显示提取的行数为 100,而总共有 527 条记录。所以没有按预期工作。
  • 您应该像我的示例一样在内部查询中使用它,然后在外部查询中的 WHERE 之前输出内部查询中的所有行数。
  • 感谢工作出色。但你的方式或这种方式更好? stackoverflow.com/questions/4007803/…
【解决方案2】:

你可以使用:

SELECT @@ROWCOUNT

查看here

【讨论】:

  • 如何将其添加到查询中?
  • 这是一个单独的查询,只需在您的查询下方使用它
  • 我检查并显示检索到的行数。所以它显示 100 不是可能结果的总数。例如总数为 527。
  • 您要检索的总数是多少?
  • 可用记录总数。我在记录之间进行分页。就像在 1-100 或 100-200 之间检索一样。有一个记录总数,例如这个查询 527。所以我也想在这个查询中获得这个数字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多