日期:2014-05-19  浏览次数:20435 次

求SQL语句一条
数据库为
ID   Word
1   a
2   b
3   d
4   e
5   f
6   g
7   h
8   i
现在我根据条件   :select   *   form   table1   where   ID=@ID
要查出复核条件的资料,和他的上一笔和下一笔,还有上两笔
谢谢大家了
很着急用呀!分少主要是我没有多的分数,谢谢大家了!

------解决方案--------------------
和他的上一笔和下一笔
select * form table1 where ID=@ID
or id=(select top 1 id from table1 where id <@id order by id desc)
or id=(select top 1 id from table1 where id> @id order by id)

和他的上两笔
select * form table1 where ID=@ID
or id in (select top 2 id from table1 where id <@id order by id desc)

和他的下两笔
select * form table1 where ID=@ID
or id in (select top 2 id from table1 where id> @id order by id)

------解决方案--------------------
reallylovesky() ( ) 信誉:100 2007-07-30 17:14:15 得分: 0


如果是最后一笔了???
怎么办?


------------------

如果是最後一筆,你就應該只要顯示兩筆了吧。

這個還是可以用。


--方法一:
Select * From (Select TOP 1 * From TEST Where ID < @ID Order By ID Desc) A
Union All
Select * From TEST Where ID = @ID
Union All
Select * From (Select TOP 1 * From TEST Where ID > @ID Order By ID ) B

--方法二:
Select * From TEST Where ID In((Select Min(ID) From TEST Where ID > @ID), @ID, (Select Max(ID) From TEST Where ID < @ID))