declare @t table(name varchar(10),xq int)
insert into @t select '花园酒店',3
insert into @t select '小天鹅酒店',2
insert into @t select '富豪酒店',4
insert into @t select '白云酒店',5
insert into @t select '逸乐酒店',1
--查询3级的
declare @n int
set @n=3--也可以改成4等任何数字
select * from @t order by case xq when @n then 0 else 1 end,xq desc

解决方案 »

  1.   

    declare @t table(name varchar(10),xq int)
    insert into @t select '花园酒店',3
    insert into @t select '小天鹅酒店',2
    insert into @t select '富豪酒店',4
    insert into @t select '白云酒店',5
    insert into @t select '逸乐酒店',1declare @n int , @m int
    select @n=3,@m =5
    select * from @t 
    order by case when xq >= @n then xq - @n else (@n - xq)*@m end
    /*
    name       xq
    ---------- -----------
    花园酒店       3
    富豪酒店       4
    白云酒店       5
    小天鹅酒店      2
    逸乐酒店       1
    */
      

  2.   

    declare @n int , @m int
    select @n=3,@m =5
    select * from @t 
    order by  (xq - @n)*(case when xq >= @n then 1 else -@m end )
    /*
    name       xq
    ---------- -----------
    花园酒店       3
    富豪酒店       4
    白云酒店       5
    小天鹅酒店      2
    逸乐酒店       1
    */
      

  3.   

    all of the above are quite right!