前两条:select top 2 * from Table where [ID] < 当前ID Order By [ID] Desc 后两条:select top 2 * from Table where [ID] > 当前ID Order By [ID]大概是这样吧。 如果不对,也可以提供个思路。
ID是连贯的吗?? 如果是的话,可以这样 SELECT * FROM TABLE WHERE ID BWTWEEN ID-2 AND ID+2
select * from table where id=指定id-2 or id=指定id-1 or id=指定id+1 or id=指定id+2
select top 2 * from tb order by id asc union all select top 2 * from tb order by id desc union all select * from tb where id='123456'
写个储存过程 create proc aa ( @pid ) as delcare @myid varchar(200) begin select * from 表 添加一个列为序号row select @myid=row from 表 where id=pid; select * from 表 row>@myid-2 and row<@myid+2 end
ID号不一定连续的啊,会有删除的情况,不能拿ID号考虑的
select top 3 * from Conection where PKID >=40 union select top 2 * from Conection where PKID<40
上面写错..select top 2 * into #temp from Conection where PKID<40 Order BY PKID DESC select * from #temp union select top 3 * from Conection where PKID >=40 drop table #temp
如果是sqlserver2005好像有解决办法,如果sqlserver2000的话,借助临时表: 首先让id连续,那就是自己生成id select identity(int,1,1) as rowid,* into #T from 表
select * from #T where rowid between a and b
select top n * from #T order by rowid desc
select top 2 * from Table where [ID] < 当前ID Order By [ID] Desc union select * from table where [id]=当前ID union select top 2 * from Table where [ID] > 当前ID Order By [ID]
16楼的显示“在关键字 'union' 附近有语法错误。”
select AA=identity(int,1,1),Customers.* into TABLEA from Customers select * from TABLEA WHERE AA IN ( (SELECT AA-1 FROM TABLEA WHERE CustomerID='???') UNION (SELECT AA-2 FROM TABLEA WHERE CustomerID='???') UNION (SELECT AA+1 FROM TABLEA WHERE CustomerID='???') UNION (SELECT AA+2 FROM TABLEA WHERE CustomerID='???') UNION (SELECT AA FROM TABLEA WHERE CustomerID='???') ) DROP TABLE TABLEA
select AA=identity(int,1,1),Customers.* into TABLEA from Customers select * from TABLEA WHERE AA IN ( (SELECT AA-1 FROM TABLEA WHERE CustomerID='???') UNION (SELECT AA-2 FROM TABLEA WHERE CustomerID='???') UNION (SELECT AA+1 FROM TABLEA WHERE CustomerID='???') UNION (SELECT AA+2 FROM TABLEA WHERE CustomerID='???') UNION (SELECT AA FROM TABLEA WHERE CustomerID='???') ) DROP TABLE TABLEA
select * from (select top 2 * from table1 where id<3 order by id asc) as tb1 union all select * from (select * from table1 where id='3') as tb2 union all select top 2 * from table1 where id>3 order by id asc
select top 2 * from Table where [ID] < 当前ID Order By [ID] Desc union all select * from table where [ID]=当前ID union all select top 2 * from Table where [ID] > 当前ID Order By [ID]
你用这个试试 MYTABLE为你目前的tableselect AA=identity(int,1,1),MYTABLE.* into NEWTABLE from Customers select * from NEWTABLE WHERE AA IN ( (SELECT AA-1 FROM NEWTABLE WHERE ID='???') UNION (SELECT AA-2 FROM NEWTABLE WHERE ID='???') UNION (SELECT AA+1 FROM NEWTABLE WHERE ID='???') UNION (SELECT AA+2 FROM NEWTABLE WHERE ID='???') UNION (SELECT AA FROM NEWTABLE WHERE ID='???') ) DROP TABLE NEWTABLE
写错了点 select AA=identity(int,1,1),MYTABLE.* into NEWTABLE from MYTABLE select * from NEWTABLE WHERE AA IN ( (SELECT AA-1 FROM NEWTABLE WHERE ID='???') UNION (SELECT AA-2 FROM NEWTABLE WHERE ID='???') UNION (SELECT AA+1 FROM NEWTABLE WHERE ID='???') UNION (SELECT AA+2 FROM NEWTABLE WHERE ID='???') UNION (SELECT AA FROM NEWTABLE WHERE ID='???') ) DROP TABLE NEWTABLE
用了楼上的显示 无法使用 SELECT INTO 语句向表 'NEWTABLE' 中添加标识列,该表中已有继承了标识属性的列 'id'。
在关键字 'union' 附近有语法错误。
你把他全部拷贝到查询分析器里边试一下 那个ID是你原表的ID
select Top 2 * from table where id < 当前ID ORDER BY ID DESC UNION ALL select Top 2 * from table where id > 当前ID ORDER BY ID ASC
这个可以select * from [table] where [id] in(select top 2 [id] from [table] where [id]<当前id Order By [id] Desc) or [id]=当前id or [id] in(select top 2 [id] from [table] where [id]>当前id Order By [id]) order by [id] desc
select * from [table] where [id] in(select top 2 [id] from [table]) or [id]=当前id or [id] in(select top 2 [id] from [table] where Order By [id] Desc) order by [id] desc
select top 2 colum from Table order by desc
后两条:select top 2 * from Table where [ID] > 当前ID Order By [ID]大概是这样吧。
如果不对,也可以提供个思路。
如果是的话,可以这样
SELECT * FROM TABLE WHERE ID BWTWEEN ID-2 AND ID+2
union all
select top 2 * from tb order by id desc
union all
select * from tb where id='123456'
create proc aa
(
@pid
)
as
delcare @myid varchar(200)
begin
select * from 表
添加一个列为序号row
select @myid=row from 表 where id=pid;
select * from 表 row>@myid-2 and row<@myid+2
end
union
select top 2 * from Conection where PKID<40
select * from #temp
union
select top 3 * from Conection where PKID >=40
drop table #temp
首先让id连续,那就是自己生成id
select identity(int,1,1) as rowid,* into #T from 表
select * from #T where rowid between a and b
select top n * from #T order by rowid desc
union
select * from table where [id]=当前ID
union
select top 2 * from Table where [ID] > 当前ID Order By [ID]
select * from TABLEA WHERE AA IN
(
(SELECT AA-1 FROM TABLEA WHERE CustomerID='???')
UNION (SELECT AA-2 FROM TABLEA WHERE CustomerID='???')
UNION (SELECT AA+1 FROM TABLEA WHERE CustomerID='???')
UNION (SELECT AA+2 FROM TABLEA WHERE CustomerID='???')
UNION (SELECT AA FROM TABLEA WHERE CustomerID='???')
)
DROP TABLE TABLEA
select * from TABLEA WHERE AA IN
(
(SELECT AA-1 FROM TABLEA WHERE CustomerID='???')
UNION (SELECT AA-2 FROM TABLEA WHERE CustomerID='???')
UNION (SELECT AA+1 FROM TABLEA WHERE CustomerID='???')
UNION (SELECT AA+2 FROM TABLEA WHERE CustomerID='???')
UNION (SELECT AA FROM TABLEA WHERE CustomerID='???')
)
DROP TABLE TABLEA
union all
select * from (select * from table1 where id='3') as tb2
union all
select top 2 * from table1 where id>3 order by id asc
union all
select * from table where [ID]=当前ID
union all
select top 2 * from Table where [ID] > 当前ID Order By [ID]
MYTABLE为你目前的tableselect AA=identity(int,1,1),MYTABLE.* into NEWTABLE from Customers
select * from NEWTABLE WHERE AA IN
(
(SELECT AA-1 FROM NEWTABLE WHERE ID='???')
UNION (SELECT AA-2 FROM NEWTABLE WHERE ID='???')
UNION (SELECT AA+1 FROM NEWTABLE WHERE ID='???')
UNION (SELECT AA+2 FROM NEWTABLE WHERE ID='???')
UNION (SELECT AA FROM NEWTABLE WHERE ID='???')
)
DROP TABLE NEWTABLE
select AA=identity(int,1,1),MYTABLE.* into NEWTABLE from MYTABLE
select * from NEWTABLE WHERE AA IN
(
(SELECT AA-1 FROM NEWTABLE WHERE ID='???')
UNION (SELECT AA-2 FROM NEWTABLE WHERE ID='???')
UNION (SELECT AA+1 FROM NEWTABLE WHERE ID='???')
UNION (SELECT AA+2 FROM NEWTABLE WHERE ID='???')
UNION (SELECT AA FROM NEWTABLE WHERE ID='???')
)
DROP TABLE NEWTABLE
无法使用 SELECT INTO 语句向表 'NEWTABLE' 中添加标识列,该表中已有继承了标识属性的列 'id'。
在关键字 'union' 附近有语法错误。
那个ID是你原表的ID
UNION ALL
select Top 2 * from table where id > 当前ID ORDER BY ID ASC
where
[id] in(select top 2 [id] from [table] where [id]<当前id Order By [id] Desc)
or [id]=当前id
or [id] in(select top 2 [id] from [table] where [id]>当前id Order By [id])
order by [id] desc
where
[id] in(select top 2 [id] from [table])
or [id]=当前id
or [id] in(select top 2 [id] from [table] where Order By [id] Desc)
order by [id] desc