MSSQL数据库,怎样一次取出指定ID号记录的前两条及后两条记录?

解决方案 »

  1.   

    select top 2 colum from Table.
    select top 2 colum from Table order by desc
      

  2.   

    不一定连续,比如要找ID是56的记录的前两条和后两条,一共5条记录,中间ID要是有删除就得要上条,反正就是一共5条
      

  3.   

    用3条语句读出来添加到一个dataTable中
      

  4.   

    前两条:select top 2 * from Table where [ID] < 当前ID Order By [ID] Desc 
    后两条:select top 2 * from Table where [ID] > 当前ID Order By [ID]大概是这样吧。
    如果不对,也可以提供个思路。
      

  5.   

    ID是连贯的吗??
    如果是的话,可以这样
    SELECT * FROM TABLE WHERE ID BWTWEEN ID-2 AND ID+2
      

  6.   

    select * from table where id=指定id-2 or id=指定id-1 or id=指定id+1 or id=指定id+2
      

  7.   

    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'
      

  8.   

    写个储存过程
    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
      

  9.   

    ID号不一定连续的啊,会有删除的情况,不能拿ID号考虑的
      

  10.   

    select top 3 * from Conection  where PKID >=40
    union
    select top 2 * from Conection where PKID<40
      

  11.   

    上面写错..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 
      

  12.   

    如果是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
      

  13.   

    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]
      

  14.   

    16楼的显示“在关键字 'union' 附近有语法错误。”
      

  15.   

    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
      

  16.   

    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
      

  17.   

    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
      

  18.   

    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]
      

  19.   

    你用这个试试
    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
      

  20.   

    写错了点
    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
      

  21.   

    用了楼上的显示
    无法使用 SELECT INTO 语句向表 'NEWTABLE' 中添加标识列,该表中已有继承了标识属性的列 'id'。
      

  22.   


    在关键字 'union' 附近有语法错误。
      

  23.   

    你把他全部拷贝到查询分析器里边试一下
    那个ID是你原表的ID
      

  24.   

    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
      

  25.   

    就是在查询分析器里执行的,表名和ID改成了我的,总显示“在关键字 'union' 附近有语法错误。”你们也可以试试啊
      

  26.   

    这个可以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
      

  27.   

    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
      

  28.   

    Union只能有一个排序规则,并且会影响到其它的查询.