select * from Tab where ID in (3,1,8,6,9,2)
这样搜出来的记录 是按ID从小到大排列的

1  ...
2  ...
3  ...
6  ...
8  ...
9  ...
但是我想就按照 3,1,8,6,9,2这个顺序排列请问各位大侠要怎么写这个sql
而且是有个参数 @ID='3,1,8,6,9,2' 传参进来的

解决方案 »

  1.   

    --(1)借用字符串进行无规律排序   
    create table #DepartMent   
    (   
     Depart varchar(10)   
    )   
    insert into #DepartMent select '组长'  
    union all select '助理'  
    union all select '总经理'  
    union all select '员工'  
    union all select '副总经理'  
    union all select '主管'  
    declare @sql varchar(100)   
    set @sql=N'总经理,副总经理,主管,组长,助理,员工'  
      
    select * from #DepartMent   
    order by charindex(N','+Depart+N',',N','+@sql+N',')  
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/02/06/3866925.aspx
      

  2.   

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/02/06/3866925.aspx
      

  3.   

    select * from Tab where ID in (3,1,8,6,9,2) 
    order by charindex(','+rtrim(ltrim(ID))+',',','+'3,1,8,6,9,2'+',')
      

  4.   

    order by id charindex(','+id+',',',3,1,8,6,9,2,')
      

  5.   

    select * from Tab order by charindex(','+ID+',' ,(',+'@ID+',')) Asc 
      

  6.   

    order by id charindex(','+ltrim(id)+',',',3,1,8,6,9,2,')
      

  7.   

    select * from Tab where ID in (3,1,8,6,9,2) 
    order by charindex(','+rtrim(ltrim(ID))+',',','+'3,1,8,6,9,2'+',')z这个不错
      

  8.   

    给个思路吧,不知道对否,我想把“3,1,8,6,9,2”分解成
    /*
    3
    1
    8
    6
    9
    */
    然后用
    select * from Tab where ID in (3) 
    union all
    select * from Tab where ID in (1) ....
    显示的效果就会是你想要的效果了