日期:2014-05-18  浏览次数:20484 次

如何让where in按in后面的顺序显示
在查询分析器中执行:
select   id   from   table1   where   id   in(3224,3225,3395,2810,3280,3281,3292)

结果确实(按ID又小到大排序了):
2810
3224
3225
3280
3281
3292
3395

请问如果能让id按我(3224,3225,3395,2810,3280,3281,3292)中的顺序显示了。

PS:我的SQL语句是通过hibernate调用的,所以不能用存储过程什么的


------解决方案--------------------
select id from table1 where id in(3224,3225,3395,2810,3280,3281,3292)
order by charindex( ', '+id+ ', ' , ', '+ '3224,3225,3395,2810,3280,3281,3292 '+ ', ')
------解决方案--------------------
select id from table1 where id in(3224,3225,3395,2810,3280,3281,3292)
order by
case id
when 3224 then 1
when 3225 then 2
when 3395 then 3
when 2810 then 4
when 3280 then 5
when 3281 then 6
when 3292 then 7
end

------解决方案--------------------
select
id
from
table1
where
id in(3224,3225,3395,2810,3280,3281,3292)
order by
charindex( ', '+rtrim(id)+ ', ' , ', '+ '3224,3225,3395,2810,3280,3281,3292 '+ ', ')
------解决方案--------------------
--老大的也可以改改
select
id
from
table1
where
id in(3224,3225,3395,2810,3280,3281,3292)
order by
charindex( ', '+rtrim(id)+ ', ' , ',3224,3225,3395,2810,3280,3281,3292, ')