给你个帮助!!查询N-M条记录?描述:要从记录集中取出中间一段记录来(如: 取10条中的第3-8条)解决思路:1. 有关键字或确保唯一的候选关键字字段:方法1:从第n条记录开始取m-n+1条数据 方法2:升序取前m条记录最为a, 再降序从a中取m-n+1条记录作为b2. 没有上述字段:增加一个自增字段生成一个临时表,在从临时表中取记录[Where 自增字段名>n-1]实例: -- --取第4到第7条记录 [对1方法一]SELECT top 4 * FROM jobs WHERE job_id not in (SELECT top 3 job_id FROM jobs)--[方法二]SELECT top 4 * FROM(SELECT top 4 * FROM(SELECT top 7 * FROM jobs ORDER BY job_id ASC) aORDER BY job_id DESC) bORDER BY job_id ASC--取第2到第3条记录 [对1方法一] select IDENTITY(int,1,1) as iid,* into #temptable from discounts select top 2 * from #temptable where iid>=2
select id=indentity(1,1),* from table into #temp1 select * from #temp1 into #temp2 where id in(5,6,7) alter table drop column id select * from #temp2
SELECT top 3 * FROM 表 WHERE id not in (SELECT top 4 id FROM 表)--注意:ID 必须能标识记录的唯一,否则就用临时表解决问题
select IDENTITY(int,1,1) as iid,* into #temptable from discounts select top 2 * from #temptable where iid>=2
select * from #temp1 into #temp2 where id in(5,6,7)
alter table drop column id
select * from #temp2