这个字段作为第一字段,再添加其他字段作为第二字段,以此类推,后面grop by 第一字段,第二字段 这样
你可以多个字段排序,比如优先销量排序,如果相同,在按照时间排序Order by 字段1,字段2,字段3
考虑优先级顺序进行多字段排序吧, 在order by 后多加一些字段进行排序 order by 字段1,字段2,字段3 desc
order by 后面多更几个条件就行了
这个问题我遇到过 你先用销量排序 然后添加 ,id desc
select top 15 [Category].title as catetitle,[Product].id,[Product].sold,[Product].userid,[Product].updatetime, [Product].shopprice,[Product].title as protitle,[Product].imgpath,[Rant].rantname,[Rant].shopendtime from [Product],[Rant],[Category] where [Product].sold < (select min ( sold) from (select top 15 [Product].sold from [Product],[Rant],[Category] where [Product].issale=1 and [Product].state=23 and [Product].userid in (4) and [Product].city=23 and [Product].parentid<>44 and [Rant].id=[Product].userid and [Category].id=[Product].parentid order by [Product].sold desc ) as tblTmp) and [Product].issale=1 and [Product].state=23 and [Product].userid in (4) and [Product].city=23 and [Product].parentid<>44 and [Rant].id=[Product].userid and [Category].id=[Product].parentid order by [Product].sold desc 比如上面的代码,我发现,按照 sold 升序排列分页,如果没有跟 主键ID,就有问题。比如,降序排列,第一页最小数据是1,第二页,其实也有1,那么,这个分页出来的结果,第二页就是小于1,就出了问题。 看来,是要跟着主键ID才可以。 where [Product].sold < (select min ( sold) from (select top 15 [Product].sold from [Product],[Rant],[Category] where [Product].issale=1 and
order by 字段1,字段2,字段3 desc
,id desc
select top 15 [Category].title as catetitle,[Product].id,[Product].sold,[Product].userid,[Product].updatetime,
[Product].shopprice,[Product].title as protitle,[Product].imgpath,[Rant].rantname,[Rant].shopendtime
from [Product],[Rant],[Category]
where [Product].sold <
(select min ( sold) from
(select top 15 [Product].sold
from [Product],[Rant],[Category]
where [Product].issale=1 and [Product].state=23 and [Product].userid in (4) and [Product].city=23 and [Product].parentid<>44 and [Rant].id=[Product].userid and [Category].id=[Product].parentid
order by [Product].sold desc ) as tblTmp)
and [Product].issale=1 and [Product].state=23 and [Product].userid in (4) and [Product].city=23 and [Product].parentid<>44 and [Rant].id=[Product].userid and [Category].id=[Product].parentid
order by [Product].sold desc
比如上面的代码,我发现,按照 sold 升序排列分页,如果没有跟 主键ID,就有问题。比如,降序排列,第一页最小数据是1,第二页,其实也有1,那么,这个分页出来的结果,第二页就是小于1,就出了问题。
看来,是要跟着主键ID才可以。
where [Product].sold <
(select min ( sold) from
(select top 15 [Product].sold
from [Product],[Rant],[Category]
where [Product].issale=1 and
USE [HeFan_cs]
GO
/****** Object: StoredProcedure [dbo].[P_GetRecordFromPage] Script Date: 07/18/2012 11:22:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GOALTER PROCEDURE [dbo].[P_GetRecordFromPage]
@tblName nvarchar(1000), -- 表名
@SelectFieldName nvarchar(4000), -- 要显示的字段名(不要加select)
@strWhere nvarchar(4000), -- 查询条件(注意: 不要加 where)
@OrderFieldName nvarchar(255), -- 排序索引字段名
@orderby nvarchar(1000), --排序字段
@PageSize int , -- 页大小
@PageIndex int = 1, -- 页码
@iRowCount int output, -- 返回记录总数
@OrderType int = 0 -- 设置排序类型, 非 0 值则降序
ASdeclare @strSQL nvarchar(4000) -- 主语句
declare @strTmp nvarchar(4000) -- 临时变量
declare @strOrder nvarchar(400) -- 排序类型
declare @strRowCount nvarchar(4000) -- 用于查询记录总数的语句
declare @temporderby nvarchar(1000) --最终排序字段set @OrderFieldName=ltrim(rtrim(@OrderFieldName))if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @OrderFieldName +' desc'
set @temporderby = ' order by ' + @orderby + ' desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @OrderFieldName +' asc'
set @temporderby = ' order by ' + @orderby + ' asc'
endset @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
+ @tblName + ' where ' + @OrderFieldName + @strTmp + '('
+ right(@OrderFieldName,len(@OrderFieldName)-charindex('.',@OrderFieldName)) + ') from (select top ' + str((@PageIndex-1)*@PageSize)
+ @OrderFieldName + ' from ' + @tblName + @strOrder + ') as tblTmp)'
+ @strOrderif @strWhere != ''
begin
set @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
+ @tblName + ' where ' + @OrderFieldName + @strTmp + '('
+ right(@OrderFieldName,len(@OrderFieldName)-charindex('.',@OrderFieldName)) + ') from (select top ' + str((@PageIndex-1)*@PageSize)
+ @OrderFieldName + ' from ' + @tblName + ' where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
if @PageIndex = 1
begin
set @strTmp = ''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere set @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
+ @tblName + @strTmp + ' ' + @strOrder
endexec(@strSQL)
print @strSQL
if @strWhere!=''
begin
set @strRowCount = 'select @iRowCount=count(*) from ' + @tblName+' where '+@strWhere
end
else
begin
set @strRowCount = 'select @iRowCount=count(*) from ' + @tblName
endexec sp_executesql @strRowCount,N'@iRowCount int out',@iRowCount out我觉得这个存储过程,没有问题啊