选择某一城市下,某一种类 ,在某种排序下的 商家列表
总攻有20000行数据
每页 20行create procedure GetShopsByCity
(
@Type tinyint,
@CityId smallint,
@SortId int,
@pageindex int,
@pagesize int
)
as
set nocount on
begin
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
insert into @indextable(nid) select Id from ShopInfo where Type=@Type AND City=@CityId
if(@SortId=1)
select O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
from ShopsList O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by O.Rate desc
else if(@SortId=2)
select O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
from ShopsList O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by CreateTime desc
else if(@SortId=3)
select O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
from ShopsList O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by CommentNumber desc
else
select O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
from ShopsList O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by ClickTimes desc
end
set nocount off几个 order by 都不能排序 ???
总攻有20000行数据
每页 20行create procedure GetShopsByCity
(
@Type tinyint,
@CityId smallint,
@SortId int,
@pageindex int,
@pagesize int
)
as
set nocount on
begin
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
insert into @indextable(nid) select Id from ShopInfo where Type=@Type AND City=@CityId
if(@SortId=1)
select O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
from ShopsList O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by O.Rate desc
else if(@SortId=2)
select O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
from ShopsList O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by CreateTime desc
else if(@SortId=3)
select O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
from ShopsList O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by CommentNumber desc
else
select O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
from ShopsList O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by ClickTimes desc
end
set nocount off几个 order by 都不能排序 ???
ALTER PROCEDURE dbo.Pagination
(
@Currentpage int,//当前页
@Pagesize int,//页大小..即每页显示多少数据
@Totalrecords int output//你所有记录的总数
)
AS
create table #temp//创建临时表,以防止你表内标识列ID不连续,即PID可以标识
(
PID int identity(1,1),
ProductID int,
ModelName varchar(50),
UnitCost money
)
insert into #temp(ProductID,ModelName,UnitCost)
select ProductID,ModelName,UnitCost
from Products//把你表内的数据插入到临时表
declare @StartID int//定义起始ID
set @StartID=(@Currentpage-1)*@Pagesize+1
declare @EndID int//定义终止ID
set @EndID=@Currentpage*@Pagesize
select * from #temp where PID>=@StartID and PID<=@EndID//取得部分ID的数据,即你想所取的部分记录的ID
set @Totalrecords=(select count(*) from #temp)//输出记录总数
大部分时间花在了 insert into @indextable(nid)上面了。
@Type tinyint,
@CityId smallint,
@SortId int,
@pageindex int,
@pagesize int
as
BEGIN
WITH ShopsListSplitPage(PageNumber,Id,[Name],Logo,Address,CommentNumber,FavoriteNumber,RecentId,RecentHead,RecentContent,Rate)
AS
(
SELECT
CEILING((ROW_NUMBER() OVER (ORDER BY SaleDate ASC))/@pagesize) AS PageNumber,
O.Id,O.[Name],O.Logo,O.Address,O.CommentNumber,O.FavoriteNumber,O.RecentId,O.RecentHead,O.RecentContent,
case
when Rate>=1 and Rate<1.25 then '1'
when Rate>=1.25 and Rate<1.75 then '1.5'
when Rate>=1.75 and Rate<2.25 then '2'
when Rate>=2.25 and Rate<2.75 then '2.5'
when Rate>=2.75 and Rate<3.25 then '3'
when Rate>=3.25 and Rate<3.75 then '3.5'
when Rate>=3.75 and Rate<4.25 then '4'
when Rate>=4.25 and Rate<4.75 then '4.5'
when Rate>=4.75 and Rate<=5 then '5'
end as Rate
FROM ShopsList O
) SELECT Id,[Name],Logo,Address,CommentNumber,FavoriteNumber,RecentId,RecentHead,RecentContent,Rate
FROM ShopsListSplitPage
WHERE PageNumber = @pageindex
END
CEILING((ROW_NUMBER() OVER (ORDER BY Rate ASC))/@pagesize) AS PageNumber,
(SELECT AVG(Rate*1.0) FROM ShopComment WHERE ShopId=ShopInfo.Id) AS Rate,
怎么在商家表里面加呢?