随风而行

^o^ 格言:相信没有做不到的事情,只有想不到的事情.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在t-sql 中使查詢結果按照自定義的字串排序

Posted on 2009-02-23 12:01  随风而行  阅读(127)  评论(0编辑  收藏  举报

-- sql server
select  id
from
(
 select '1' as id union all
 select '2' union all
 select '3' union all
 select '4' union all
 select '5'
)   t
order by case when charindex(id, '3, 2, 4, 1') = 0 then 1000000 else charindex(id, '3, 2, 4, 1') end


-- oracle
select  id
from
(
    select '1' as id from dual union all
    select '2' from dual union all
    select '3' from dual union all
    select '4' from dual union all
    select '5' from dual
)   t
order by case when instr('3, 2, 4, 1', id) = 0 then 1000000 else instr('3, 2, 4, 1', id) end