select * from t_question where id in ('5','3','4','8','39')
'5','3','4'是ID号,
这样查出来给自动排序了,
3
4
5
8
39
我想按照我查询的条件排序,如下
5
3
4
8
39
要怎么写??

解决方案 »

  1.   

    select * from t_question where id in ('5','3','4','8','39') 
    order by id
      

  2.   


     declare @t table( [id] int ) insert into @t values( 3)
     insert into @t values( 4)
     insert into @t values( 5)
     insert into @t values( 39)
     insert into @t values( 53)
      
     select * from @t where id in ('5','3','4','39')
     order by PATINDEX( '%'+ Convert(varchar(30),[id])+',%','5,3,4,39,')
      

  3.   

    把in拆開成unionselect * from t_question where id ='5'
    union
    select * from t_question where id ='3'
    union
    select * from t_question where id ='4'
    union
    select * from t_question where id ='8'
    union
    select * from t_question where id ='39'
      

  4.   

    在存储中定义一个按你顺序排列值的数组.然后用循环
    读取相应id,继而从表里读取数据.你这个想法在id量少时还行,太多了就很糟糕了.最好是根据另一个字段来排序,
      

  5.   

    那就在你的查询语句后面加上order by 你的条件,就行了。
      

  6.   

    declare @id
    set @id='5,3,4,39,'
    declare @t table( [id] int ) insert into @t values( 3)
     insert into @t values( 4)
     insert into @t values( 5)
     insert into @t values( 39)
     insert into @t values( 53)
      
     select * from @t where id in (@id.substring(@id,@id.len-1))
     order by PATINDEX( '%'+ Convert(varchar(30),[id])+',%',@id)