name  age  state
小明  26    丙
小黄  24    乙
小青  22    甲
小龙  34    乙
小张  26    甲
小玉  35    丙这个表的样子,我要按最后列  甲,乙,丙 的顺序排列,怎么做比较好,效率比较高!

解决方案 »

  1.   

    我不知道。
    如果是我我只能
     
    select甲的 
    union all
    select已的 
    union all
    select丙的 呵呵
      

  2.   

    最好再给state 加个字段标识顺序,如stateOrder
      

  3.   


    Create table #t(name varchar(10),age int,state varchar(10))
    insert  #t  select '小明',26,'丙' union all
    select '小黄',24,'乙' union all
    select '小青',22,'甲' union all
    select '小龙',34,'乙' union all
    select '小张',26,'甲' union all
    select '小玉',35,'丙'select * from #tselect name,age,state,sort=case state when '甲' then 1 when '乙' then 2 when '丙' then 3 else 4 end from #t order by sort asc
    drop table #t
      

  4.   


    如果是 甲甲甲甲乙乙乙乙丙丙丙丙丙这样排序,就这样
    SELECT name,  age  ,state  FROM table where state='甲'
    union all 
    SELECT name,  age  ,state  FROM table where state='乙'
    union all 
    SELECT name,  age  ,state  FROM table where state='丙'如果是 甲乙丙这样排序,就这样SELECT name,  age  ,state  FROM table order by charindex(state,'甲乙丙')
      

  5.   

    SELECT name,  age  ,state  FROM table order by charindex(','+ltrim(state)+',',',甲,乙,丙,')