方法一:取出前n条和前m条然后取出其差(n-m) select top(n-m) * from table where 关键字段 not in(select top m 关键字段 from table)方法二:先取出前n条的,再倒过来取出n-m就可以 select top(n-m+1) * from (select top n * from table order by 关键字段 desc) order by 关键字段
--如果是SQL Server数据库查询第X页,每页Y条记录最基本的处理方法:如果表中有主键(记录不重复的字段也可以),可以用类似下面的方法,当然y,(x-1)*y要换成具体的数字,不能用变量:select top y * from 表 where 主键 not in(select top (x-1)*y 主键 from 表)如果表中无主键,可以用临时表,加标识字段解决.这里的x,y可以用变量.select id=identity(int,1,1),* into #tb from 表 select * from #tb where id between (x-1)*y and x*y-1
同意yoki(小马哥--鬓微霜,又何妨)的说法,其方法1应该没有问题,方法2略改动一点.原来: 方法二:先取出前n条的,再倒过来取出n-m就可以 select top(n-m+1) * from (select top n * from table order by 关键字段 desc) order by 关键字段现在: select top(n-m+1) * from (select top n * from table order by 关键字段 desc) as 一个别名(只要不是关键字就可以了) order by 关键字段
想问问:在下面这个语句里面如何加入查询条件。方法一:取出前n条和前m条然后取出其差(n-m) select top(n-m) * from table where 关键字段 not in(select top m 关键字段 from table)在这个问题上折腾二三天了,快烦死! 另还开了一贴: http://expert.csdn.net/Expert/topic/2853/2853224.xml?temp=.6555902
这是我的表结构:TxStatistics:通讯记录统计表 栏位名称 内容 类型 txdate 交易日期 datetime line_no 线路号码 Char(4) scdt 数据传输成功数量 int scht 心跳测试成功数量 int fldt 数据传输失败数量 Int flht 心跳测试失败数量 int status 当前网络状况 Char(1)Index: txdate + line_no我写成以下这样,就没问题 select top 6 * from TxStatistics where txdate + scdt not in (select top 3 txdate + scdt from TxStatistics order by txdate desc) and txdate <= '2004-3-17' and line_no = '06' order by txdate desc把以上的scdt改为line_no: select top 6 * from TxStatistics where txdate + line_no not in (select top 3 txdate + line_no from TxStatistics order by txdate desc) and txdate <= '2004-3-17' and line_no = '06' order by txdate desc 运行就会说: 服务器: 消息 241,级别 16,状态 1,行 1 从字符串转换为 datetime 时发生语法错误。
select top(n-m) *
from table
where 关键字段 not in(select top m 关键字段 from table)方法二:先取出前n条的,再倒过来取出n-m就可以
select top(n-m+1) *
from (select top n * from table order by 关键字段 desc)
order by 关键字段
再取自增长ID大于N的记录亦可.
select * from #tb where id between (x-1)*y and x*y-1
http://expert.csdn.net/Expert/topic/2365/2365596.xml?temp=.8605615
方法二:先取出前n条的,再倒过来取出n-m就可以
select top(n-m+1) *
from (select top n * from table order by 关键字段 desc)
order by 关键字段现在:
select top(n-m+1) *
from (select top n * from table order by 关键字段 desc) as 一个别名(只要不是关键字就可以了)
order by 关键字段
select top(n-m) * from table
where 关键字段 not in(select top m 关键字段 from table)在这个问题上折腾二三天了,快烦死!
另还开了一贴:
http://expert.csdn.net/Expert/topic/2853/2853224.xml?temp=.6555902
http://expert.csdn.net/Expert/topic/2853/2853838.xml?temp=.9094355我已经快疯掉了!:(
栏位名称 内容 类型
txdate 交易日期 datetime
line_no 线路号码 Char(4)
scdt 数据传输成功数量 int
scht 心跳测试成功数量 int
fldt 数据传输失败数量 Int
flht 心跳测试失败数量 int
status 当前网络状况 Char(1)Index: txdate + line_no我写成以下这样,就没问题
select top 6 * from TxStatistics where txdate + scdt not in (select top 3 txdate + scdt from TxStatistics order by txdate desc) and txdate <= '2004-3-17' and line_no = '06' order by txdate desc把以上的scdt改为line_no:
select top 6 * from TxStatistics where txdate + line_no not in (select top 3 txdate + line_no from TxStatistics order by txdate desc) and txdate <= '2004-3-17' and line_no = '06' order by txdate desc
运行就会说:
服务器: 消息 241,级别 16,状态 1,行 1
从字符串转换为 datetime 时发生语法错误。