查找一个人在排行表中的名次,和在这个人的前五名与后五名的人。SQL语句怎么写?sql

解决方案 »

  1.   

    用row_number排个序,然后找到你要查找的人的序列号的前五和后五即可
      

  2.   

    写了一个事例你看一下吧。declare @num as int = 100
    ;
    with T as
    (
    select number,paiming = ROW_NUMBER() over(order by number)
    from master..spt_values
    where type = 'P'
    )
    select * from T
    where  ABS( @num - number) <= 5
      

  3.   

    借用系统表,按绝对值小于等于5,是个思路,楼上的语句貌似只能支持sql2005
      

  4.   

    我用的是mysql,请问怎么写好
      

  5.   

    这样?先得出row=名次;with Cte
    as
    (select *,dense_rank()over(order by 名次) as row from 表名)select a.* from Cte as a ,Cte as b  
    where b.Name='张三' and abs(a.row-b.row) between 1 AND 5--已存在名次列
    select a.* from 表 as a ,表 as b  
    where b.Name='张三' and abs(a.名次-b.名次) between 1 AND 5
      

  6.   

    排序后,找到前5条,再找到后5条
    使用union将两个集合合成一个