在查询分析器中执行:
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调用的,所以不能用存储过程什么的

解决方案 »

  1.   

    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'+',')
      

  2.   

    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
      

  3.   

    order by charindex(...) 是好方法:)
      

  4.   

    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'+',')
      

  5.   

    --老大的也可以改改
    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,')
      

  6.   

    select table1.id from table1 inner join (select 3224 id union all select 3225 union all select 3395 union all select 2810 union all select 3280 union all select 3281 union all select 3292) table2 on table1.id=table2.id
      

  7.   

    &apos  这个是什么呀???
      

  8.   

            SQL = "Select top 3 * from A_Product where ID in (Select A_Product from A_Commend where A_Class ='man') order by charindex(ID,'1,3,5')"测试字符帖,大家不要看.