如一列数据(2,5,8,4,6,7,5)我想根据自定义序列(5,8,6,7,2,4)这样一个顺序输出结果5,5,8,6,7,2,4 请解决办法SQL

解决方案 »

  1.   

    如果你的自定义毫无规则,那么只能再创建一个表,然后先排好序,再在排序的时候,关联这个表。或者在order by的时候用直接写死你的顺序。
      

  2.   

    按照指定的字符排序,给你个例子:
    --c2列的数据按'4','1','2'的指定顺序排序
    create table t_orderby
    (
    c1 int          null,
    c2 varchar(10)  null,
    c3 varchar(10)  null
    )
    go
    insert into t_orderby
    select 1,'2','a1'  union all
    select 1,'1','a2'  union all
    select 3,'1','ab'  union all
    select 1,'4','b1'
    go
    select * from t_orderby
     order by charindex(c2,'4,1,2')详见:http://qianzhang.blog.51cto.com/317608/1201724