create table #temp
(id varchar(50)
)
insert into #temp
select '1' union all select '2' union all select '3' union all select '4' union all select '5' union all select '6' union all select '7' union all select '8'
select * from #temp
----------------------
id
1
2
3
4
5
6
7
8
查询前后两位ID
select * from #temp
where
[id] in(select distinct top 2 [id] from #temp where [id]<5 order by [id] DESC)
or
[id]=5
or
[id] in(select distinct top 2 [id] from #temp where [id]>5 order by [id] ASC)
-------------
id
3
4
5
6
7
select * from #temp
where
[id] in(select distinct top 范围数 [id] from #temp where [id]<参照id order by [id] DESC)
or
[id]=参照id
or
[id] in(select distinct top 范围数 [id] from #temp where [id]>参照id order by [id] ASC)
select * from tablename
where
id in(select distinct top 范围数 [id] from tablename where id<参照id order by id DESC)
or
id in(select distinct top 范围数 [id] from tablename where id>参照id order by id ASC)
select top N * from tbName where id<'' order by id desc
union all
select * from tbName where id=''
union all
select top N * from tbName where id>'' order by id