sql语句查询并且加分页
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql语句查询并且加分页相关的知识,希望对你有一定的参考价值。
--ReadCount
select top 5 * from T_CommonProblems where ReadCount>10 order by ReadCount desc
--weight
select top (15-(select COUNT(1) from (select top 5 * from T_CommonProblems where ReadCount>10) as tb )) * from T_CommonProblems where ID not in (select top 5 ID from T_CommonProblems where ReadCount>10) order by Weight desc
先根据readcount越高排在最前面,之后在根据weight降序排序,(每页十五行,前五行是readcount降序,后十行是根据weight降序,如果数量不够,则相应补齐)上面是效果已经出来,能不能把这两句的sql语句整合在一起并且加分页,每页显示15行, 在线等。很急,谢谢!!!!
readcount 只取大于10.
如果整合在一起不行的话,也可以分别加分页,并且最好可以把weight的sql语句稍微简化些。
select top 5 id,readcount,weight from table1 order by readcount desc
union all
select top 10,id,readcount,weight from table1 order by weight desc --这句进行动态sql或传参数进行分页,网上分页的sql很多。有问题再追问。 参考技术B 如果是 SQL Server 数据库的话, 你可以尝试 创建个返回结果集的存储过程, 来完成这个操作。追问
不太懂,我可是新手
追答CREATE FUNCTION 表值函数的名字 ()
RETURNS @result TABLE( 你查询结果里面的列的定义 )
AS
BEGIN
INSERT INTO @result select top 5 * from T_CommonProblems where ReadCount>10 order by ReadCount desc;
INSERT INTO @result select top (15-(select COUNT(1) from (select top 5 * from T_CommonProblems where ReadCount>10) as tb )) * from T_CommonProblems where ID not in (select ID from @result) order by Weight desc ;
RETURN;
END;
查询的方法:
SELECT * FROM 表值函数的名字();
以上是关于sql语句查询并且加分页的主要内容,如果未能解决你的问题,请参考以下文章