mysql 查询某个字段的上一行或者下一行

解决方案 »

  1.   

    N-M条记录
    1.
    select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
    set rowcount n
    select * from 表变量 order by columnname desc
    2.
    select top n * from 
    (select top m * from tablename order by columnname) a
    order by columnname desc
    3.
    如果tablename里没有其他identity列,那么:
    select identity(int) id0,* into #temp from tablename取n到m条的语句为:
    select * from #temp where id0 >=n and id0 <= m如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
    exec sp_dboption 你的DB名字,'select into/bulkcopy',true
    4.
    如果表里有identity属性,那么简单:
    select * from tablename where identitycol between n and m
      

  2.   

    --MSSQL
    select * from 
    (select top 1 * from tb where id>5 order by id asc)a
    union all
    select * from 
     (select top 1 * from tb where id<5 order by id desc)b
      

  3.   


    小F mssql这样的语句是什么?
      

  4.   

    MYSQL 也可以用自增列可以试试:appy_Stone 的思路应该可行
      

  5.   

    mysql 查询某个字段的上一行或者下一行什么是 某个字段 的上一行? (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。