参见下属范例代码:
select * from table1 where id in (3,45,123,54,2)
order by charindex(','+cast(id as varchar)+',',',3,45,123,54,2,')
或者也可以是
order by case when id when 3 then 1 when 45 then 2 when 123 then 3 when 54 then 4 when 2 then 5 end
博客给出SQL查询按指定顺序排序的范例代码。一是使用charindex函数,如select * from table1 where id in (3,45,123,54,2) order by charindex(','+cast(id as varchar)+',',',3,45,123,54,2,');二是使用case when语句。
参见下属范例代码:
select * from table1 where id in (3,45,123,54,2)
order by charindex(','+cast(id as varchar)+',',',3,45,123,54,2,')
或者也可以是
order by case when id when 3 then 1 when 45 then 2 when 123 then 3 when 54 then 4 when 2 then 5 end
1834
901

被折叠的 条评论
为什么被折叠?