求指导一条SQL语句。获取指定记录的前后几条记录
本帖最后由 xjkstar 于 2013-01-15 14:17:12 编辑 在sql 中 要获得当前值前后100条记录,sql语句怎么写?
比如当前值是B100,那么结果就是:
B000、B001……B099(前99条)、B100、(后100条)B101……B199、B200.
我是这么写的
SELECT num FROM table1
WHERE num IN
(
( select top 100 num from table1
where num <= 'B100' order by num desc
)
union
(
select top 100 num from table1
where num > 'B100' order by num
)
)
order by num
SELECT num FROM table1
WHERE num IN
(
( select top 100 num from table1
where num <= 'B100' order by num --desc,这里的倒序被注释掉
)
union
(
select top 100 num from table1
where num > 'B100' order by num
)
)
order by num