select * from table where id in (3,4,1,2,5)
这样查出来的结果会按照table的自增主键ID排序,即查出的结果会是1,2,3,4,5
而我希望查出的结果按照(3,4,1,2,5)列出,也第一条id=3,...
应该怎么写这个SQL语句啊,是要创建个临时表,再联合查询吗

解决方案 »

  1.   

    select * from table order by charindex(id,'3,4,1,2,5')
      

  2.   

    select * from table where id in (3,4,1,2,5)
    order by case id when 3 then 1 when 4 then 2 when 1 then 3 when 2 then 4 when 5 then 5 end
      

  3.   

    select * from table where id in (3,4,1,2,5) order by charindex(','+ltrim(id)+',', ',3,4,1,2,5,')