在使用order by 排序的时候。
用case when 或者charindex这种方式。
如:一个字段中有(A,张,王,B)
order by case when  'A' then 1, case  when '王' then 2 end (后面 张,B按照默认排序) A,王,B,张
这个怎么实现。我用上面那个语句后,没有条件的自动跑到这个去了。

解决方案 »

  1.   


    order by 
      case 
        when col='A' then 1 
        when col='王' then 2 
        else 3 
      end,
    col
      

  2.   

    如果用charindex这个函数排序呢?
      

  3.   


    order by charindex(col,'王A') desc,col
      

  4.   


    declare @T table (col varchar(2))
    insert into @T
    select 'A' union all
    select '张' union all
    select '王' union all
    select 'B' union all
    select 'c' union all
    select '李'select * from @T order by 
    case col when 'A' then 1 when '张' then 2 when '王' then 3 when 'B' then 4 else 5 end/*
    col
    ----
    A


    B
    c

    */
    declare @T table (col varchar(2), [order] int)
    insert into @T
    select 'A',1 union all
    select '张',2 union all
    select '王',3 union all
    select 'B',4 union all
    select 'c',5 union all
    select '李',6select col from @T order by [order]