完整的加上分页查询语句
with temptbl as (SELECT ROW_NUMBER() OVER (ORDER BY Generate_time desc ) AS Row, * from [OweMaterial] o where exists (
SELECT 1 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is not NULL and [Y_N_Generate] is NULL
union
SELECT 2 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is NULL and [Y_N_Generate] is NULL
union
SELECT 3 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is not NULL and [Y_N_Generate] is not NULL ) )
SELECT * FROM temptbl where Row between (1-1)*100+1 and (1-1)*100+100 order by Y_N_Generate desc ,reply_text descgroupID Id mr_ItemCode ------- DocEntry ---还有很多列名1 ----- 20 A101-0603R-102J --- 5
1 ----- 57 A101-0603R-103J --- 8
1 ----- 83 A101-0603R-1101F -- 188
2 ----- 11 A101-0603R-1002F -- 848
2 ----- 13 S111-SERVICE-WLF -- 3
2 ----- 23 A101-0603R-101J --- 5
3 ----- 11 A101-0603R-1002F -- 848
3 ----- 13 S111-SERVICE-WLF -- 3
3 ----- 23 A101-0603R-101J --- 5
如何这样的效果,是要加分页的查询
groupID这一列是数据库中没有是根据这三条件查出的
SELECT 1 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is not NULL and [Y_N_Generate] is NULL
union
SELECT 2 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is NULL and [Y_N_Generate] is NULL
union
SELECT 3 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is not NULL and [Y_N_Generate] is not NULL 但是,我加上这个分页查询看就不,没有见到groupID这一列 值了,为什么啊!,,,我的SQL学得很差的分页的查询语句
with temptbl as (SELECT ROW_NUMBER() OVER (ORDER BY Generate_time desc ) AS Row, * from [OweMaterial] o where exists (这里就是要查询的条件,groupID也是在这里面的) )
SELECT * FROM temptbl where Row between (1-1)*100+1 and (1-1)*100+100 order by Y_N_Generate desc ,reply_text desc
请大虾们,指点一下,帮帮啊!
谢谢!!
with temptbl as (SELECT ROW_NUMBER() OVER (ORDER BY Generate_time desc ) AS Row, * from [OweMaterial] o where exists (
SELECT 1 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is not NULL and [Y_N_Generate] is NULL
union
SELECT 2 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is NULL and [Y_N_Generate] is NULL
union
SELECT 3 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is not NULL and [Y_N_Generate] is not NULL ) )
SELECT * FROM temptbl where Row between (1-1)*100+1 and (1-1)*100+100 order by Y_N_Generate desc ,reply_text descgroupID Id mr_ItemCode ------- DocEntry ---还有很多列名1 ----- 20 A101-0603R-102J --- 5
1 ----- 57 A101-0603R-103J --- 8
1 ----- 83 A101-0603R-1101F -- 188
2 ----- 11 A101-0603R-1002F -- 848
2 ----- 13 S111-SERVICE-WLF -- 3
2 ----- 23 A101-0603R-101J --- 5
3 ----- 11 A101-0603R-1002F -- 848
3 ----- 13 S111-SERVICE-WLF -- 3
3 ----- 23 A101-0603R-101J --- 5
如何这样的效果,是要加分页的查询
groupID这一列是数据库中没有是根据这三条件查出的
SELECT 1 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is not NULL and [Y_N_Generate] is NULL
union
SELECT 2 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is NULL and [Y_N_Generate] is NULL
union
SELECT 3 as groupID, * from [OweMaterial] o
where [From_User_Name] ='yoyo' and Reply_text is not NULL and [Y_N_Generate] is not NULL 但是,我加上这个分页查询看就不,没有见到groupID这一列 值了,为什么啊!,,,我的SQL学得很差的分页的查询语句
with temptbl as (SELECT ROW_NUMBER() OVER (ORDER BY Generate_time desc ) AS Row, * from [OweMaterial] o where exists (这里就是要查询的条件,groupID也是在这里面的) )
SELECT * FROM temptbl where Row between (1-1)*100+1 and (1-1)*100+100 order by Y_N_Generate desc ,reply_text desc
请大虾们,指点一下,帮帮啊!
谢谢!!
SET @pagenum = 2
SET @pagesize = 3
SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY newsid DESC) AS rownum,
newsid, topic, ntime, hits
FROM news) AS D
WHERE rownum BETWEEN (@pagenum-1)*@pagesize+1 AND @pagenum*@pagesize
ORDER BY newsid DESC aspx里面只需给SQL传入pageid和条数即可。 CSDN上还有个存储过程实现分页的代码:
ALTER PROCEDURE news_Showlist
(
@tblName varchar(255), -- 表名
@strGetFields varchar(1000), -- 需要返回的列
@fldName varchar(255), -- 排序的字段名
@PageSize int , -- 页尺寸
@PageIndex int , -- 页码
@strWhere varchar(1500), -- 查询条件(注意: 不要加where)
@Sort varchar(255) --排序的方法)
AS
declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型if @Sort = 'desc'
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @fldName +' desc'
--如果@OrderType不是,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @fldName +' asc'
end
if @PageIndex = 1
begin
if @strWhere != ''
begin
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName
+ ' where ' + @strWhere + ' ' + @strOrder
end
else
begin
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @tblName
+ ' '+ @strOrder
end
--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
+ @tblName + ' where ' + @fldName + '' + @strTmp + '('+ @fldName + ') from
(select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from '
+ @tblName + '' + @strOrder + ') as tblTmp)'+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
+ @tblName + ' where ' + @fldName + '' + @strTmp + '('+ @fldName + ') from
(select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from '
+ @tblName + ' where ' + @strWhere + ' '+ @strOrder + ') as tblTmp)
and ' + @strWhere + ' ' + @strOrder
end
exec (@strSQL)
RETURN
SELECT TOP 页记录数量 *
FROM 表名
WHERE (ID NOT IN
(SELECT TOP (每页行数*(页数-1)) ID
FROM 表名
ORDER BY ID))
ORDER BY ID
//自己还可以加上一些查询条件
例:
select top 2 *
from Sys_Material_Type
where (MT_ID not in
(select top (2*(3-1)) MT_ID from Sys_Material_Type order by MT_ID))
order by MT_ID 2.分页方案二:(利用ID大于多少和SELECT TOP分页) 语句形式:
SELECT TOP 每页记录数量 *
FROM 表名
WHERE (ID >
(SELECT MAX(id)
FROM (SELECT TOP 每页行数*页数 id FROM 表
ORDER BY id) AS T)
)
ORDER BY ID 例:
SELECT TOP 2 *
FROM Sys_Material_Type
WHERE (MT_ID >
(SELECT MAX(MT_ID)
FROM (SELECT TOP (2*(3-1)) MT_ID
FROM Sys_Material_Type
ORDER BY MT_ID) AS T))
ORDER BY MT_ID 3.分页方案三:(利用SQL的游标存储过程分页)
create procedure SqlPager
@sqlstr nvarchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int --每页行数
as
set nocount on
declare @P1 int, --P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1, @rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off 4.总结:
其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。
建议优化的时候,加上主键和索引,查询效率会提高。 通过SQL 查询分析器,显示比较:我的结论是:
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句
分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接SQL语句
分页方案三:(利用SQL的游标存储过程分页) 效率最差,但是最为通用
1 ----- 57 A101-0603R-103J --- 8
1 ----- 83 A101-0603R-1101F -- 188
2 ----- 11 A101-0603R-1002F -- 848
2 ----- 13 S111-SERVICE-WLF -- 3
2 ----- 23 A101-0603R-101J --- 5
3 ----- 11 A101-0603R-1002F -- 848
3 ----- 13 S111-SERVICE-WLF -- 3
3 ----- 23 A101-0603R-101J --- 5
如何查出这样的效果 分页先不管了
这个你没有搞过吗:?这个问题很难吗,,,,,,,我的SQL不行啊大虾们,顶啊,,,帮忙啊!!!!!!