比如数据库中有这样的id字段,且是主键,以下为删除记录之后的id的编号:
1
3
4
9
21
35
38
想问一下如何得到当前记录的上一条和下一条记录呢?
比如当前记录是9如何通过sql语句得到下一条(21)上一条(4)呢.?谢谢帮忙,虽然这个问题有点刺手,还
是请大家帮我一下,谢谢了

解决方案 »

  1.   

    ms sql: 
    select top 1 id as preid from table a where a.id < 9 order by a.id desc
    union all
    select top 1 id as nextid from table a where a.id > 9 order by a.idoracle:
    select id as preid from table a where a.id < 9 and rownum = 1 order by a.id desc
    union all
    select id as nextid from table a where a.id > 9 and rownum = 1 order by a.id
      

  2.   

    select   top   1   *   from   article   where   insro='pro'   and     id<9   order   by   ID   desc    
        
        
    select   top   1   *   from   article   where   insro='pro'   and     id>9 order   by   ID
      

  3.   

    是不是可以先通过IDENTITY()函数加上有序的序列号,然后再判断就应该能可以了
      

  4.   

    先不结贴,不好意思,不明白seawolfover的意思
      

  5.   

    select top 1 id from xx where id<9 order by id desc
    上一条,下一条也类似
      

  6.   

    select   identity(int,1,1)   as   id,*   into   table2   from   table1   
     在table2中加一列ID从1开始,2.3.4......N
      

  7.   

    看大家这么热情,本来想现在就结帐的,大家的意思我懂,但就是不明白seawolflover的意思,请大家帮忙分析一下,他的思路,搞懂他的思路马上结帐.
      

  8.   

    IDENTITY(int,   1,   1)   数据类型int,从1开始,步长为1递增。   
      本函数只可与SELECT   INTO   表   一起用。   
        
      select   identity(int,1,1)   as   id,*   into   table2   from   table1   
      在table2中加一列ID从1开始,2.3.4......N