在t-sql 中使查詢結果按照自定義的字串排序
-- 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