select * from tablename order by a2,a4

解决方案 »

  1.   

    order by charindex(title,'a2a4a3a1a5')
      

  2.   

    CHARINDEX
    返回字符串中指定表达式的起始位置。 语法
    CHARINDEX ( expression1 , expression2 [ , start_location ] ) 参数
    expression1一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。expression2一个表达式,通常是一个用于搜索指定序列的列。expression2 属于字符串数据类型分类。start_location在 expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2 的起始位置开始搜索。
      

  3.   

    --测试:
    create table t1(id int,title char(2))
    insert into t1 select '1','a1'
    insert into t1 select '2','a2'
    insert into t1 select '3','a3'
    insert into t1 select '4','a4'
    insert into t1 select '5','a5'select * from t1 order by charindex(title,'a2a4a3a1a5')
    /*
    id          title 
    ----------- ----- 
    2           a2
    4           a4
    3           a3
    1           a1
    5           a5(所影响的行数为 5 行)*/
      

  4.   

    order by case title when 'a2' then 0
                        when 'a4' then 1
                        when 'a3' then 2 
                        else 3 end,id
      

  5.   

    /*--补充一下  马可的方法,需要将所有的title列出
      如果楼主只是按照:title = 'a2' or title = 'a4' or title = 'a3'
      其余的排在后面,并按title从大到小排序,或不排序
      就用下面的处理方法--*/declare @t1 table(id int,title char(2))
    insert into @t1 select '1','a1'
    insert into @t1 select '2','a2'
    insert into @t1 select '3','a3'
    insert into @t1 select '4','a4'
    insert into @t1 select '5','a5'select * from @t1 
    order by charindex(title,'a3a4a2') desc  --先按指定顺序排,注意,与马可的不同,是反过来写
      ,title  --如果其余的要排序,就加此句,否则不要此句./*--测试结果
    id          title 
    ----------- ----- 
    2           a2
    4           a4
    3           a3
    1           a1
    5           a5(所影响的行数为 5 行)
    --*/
      

  6.   


    order by charindex(','+title+',',',a3,a4,a2,') desc
    否则a3,a4,a2内容相互包含就不对了.
      

  7.   

    蒋老师的最完整:
    或者干脆用 case when 这种要求的贴子有一段时间经常出现的,不过这一阵没怎么见到了。