随机排序分页处理示例
2008-04-09 03:44
183 查看
/*--原帖地址:
http://community.csdn.net/Expert/topic/3845/3845647.xml?temp=.7272455
--*/
/*--处理要求
用如下语句可以实现随机排序:
select * from xiaofei where status=1 order by newid()
这样的话每次用户刷新页面排序就会变.
现在我想做成不同用户进来页面看到的排序都不一样,但是同一个用户在一段时间内每次刷新页面看到的排序都是一样的
--*/
go
/*--处理分析
借助临时表缓存排序结果,再结合原表主键查询数据就可以了
--*/
--大致的处理存储过程
create proc p_qry
@username sysname, --用户名,根据用户名来设定排序处理的临时表
@pagesize int=5, --每页大小
@currentpage int=1 --当前页,>=1表示正常查询,<1表示重建临时表
as
set nocount on
declare @tbname sysname
set @tbname=quotename('##'+@username)
--如果临时表不存在,或者@currentpage<1,则生成处理临时表
if object_id(@tbname) is null or isnull(@currentpage,0)<1
begin
--如果临时表已经存在,先删除它
if object_id(@tbname) is not null exec('drop table '+@tbname)
--假设表的主键字段名为: id
exec('select nnid=identity(bigint,0,1),* from(
select top 100 percent id from xiaofei
where status=1
order by newid())a')
set @currentpage=1
end
--根据需要查询指定页的数据
declare @s nvarchar(4000)
set @s='select a.* from xiaofei a,'+@tbname+' b
where a.id=b.id
and b.nnid between (@currentpage-1)*@pagesize+1
and @currentpage*@pagesize'
exec sp_executesql @s,N'@currentpage int,@pagesize int',@currentpage,@pagesize
http://community.csdn.net/Expert/topic/3845/3845647.xml?temp=.7272455
--*/
/*--处理要求
用如下语句可以实现随机排序:
select * from xiaofei where status=1 order by newid()
这样的话每次用户刷新页面排序就会变.
现在我想做成不同用户进来页面看到的排序都不一样,但是同一个用户在一段时间内每次刷新页面看到的排序都是一样的
--*/
go
/*--处理分析
借助临时表缓存排序结果,再结合原表主键查询数据就可以了
--*/
--大致的处理存储过程
create proc p_qry
@username sysname, --用户名,根据用户名来设定排序处理的临时表
@pagesize int=5, --每页大小
@currentpage int=1 --当前页,>=1表示正常查询,<1表示重建临时表
as
set nocount on
declare @tbname sysname
set @tbname=quotename('##'+@username)
--如果临时表不存在,或者@currentpage<1,则生成处理临时表
if object_id(@tbname) is null or isnull(@currentpage,0)<1
begin
--如果临时表已经存在,先删除它
if object_id(@tbname) is not null exec('drop table '+@tbname)
--假设表的主键字段名为: id
exec('select nnid=identity(bigint,0,1),* from(
select top 100 percent id from xiaofei
where status=1
order by newid())a')
set @currentpage=1
end
--根据需要查询指定页的数据
declare @s nvarchar(4000)
set @s='select a.* from xiaofei a,'+@tbname+' b
where a.id=b.id
and b.nnid between (@currentpage-1)*@pagesize+1
and @currentpage*@pagesize'
exec sp_executesql @s,N'@currentpage int,@pagesize int',@currentpage,@pagesize
相关文章推荐
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- [导入]随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- 随机排序分页处理示例
- solr复杂查询;及代码示例;包括查询、过滤、分页、排序、高亮显示等处理 的代码示例;
- 数据库查询排序使用随机排序结果示例(Oracle/MySQL/MS SQL Server)
- MySQL数据库limit分页、排序-SQL语句示例
- 基于Metronic的Bootstrap开发框架经验总结(16)-- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理
- 树形数据深度排序处理示例(模拟单编号法).sql