如题使用in关键字查询多条记录,但希望查询结果不要自动排序。例句:
select ID, Name  from stu where ID in (2,3,1) 
----查询输出结果:
ID Name
1 aaaa
2 bbbb
3 cccc----我要的结果是:
ID Name
2 bbbb
3 cccc
1 aaaa

解决方案 »

  1.   

    select ID, Name  from stu where ID in (2,3,1) order ID asc
      

  2.   

    select ID, Name  from stu where ID in (2,3,1) 
    order by charindex(',+ltrim(id)+,',',2,3,1,')
      

  3.   

    select ID, Name  from stu where ID in (2,3,1) 
    ORDER BY CHARINDEX(','+LTRIM(ID)+',',',2,3,1,')
      

  4.   

    select ID, Name  from stu where ID in (2,3,1) 
    order by charindex(','+ltrim(id)+',',',2,3,1,')
      

  5.   

    多谢,这是sqlserver中可以用的吧。
    我目前使用的是access好像不支持的。
    【表达式中 'charindex' 函数未定义。】
      

  6.   

    create table #test (ID    int,Name varchar(10))
    insert #test select 1    ,'aaaa'
    insert #test select 2    ,'bbbb'
    insert #test select 3    ,'cccc'select * from #test where ID in (2,3,1) 
    order by charindex(','+ltrim(id)+',',',2,3,1,')
             ID Name
    ----------- ----------
              2 bbbb
              3 cccc
              1 aaaa(3 行受影响)啊ACCESS
      

  7.   

    不支持呀,in里面的数据多不?不多改成union all 连接吧,别的不清楚
      

  8.   


    恩,不很多。用union all怎么写?
      

  9.   

    Access 不支持charindex,没有其他方法了。
    还有说用union all 连接的,我试试这个。要这么干?主要是按选择时的顺序显示。
    当选择多个时,不想一个个查询,但也不能改变选择时的顺序,
    所以才想办法这sql语句上想办法。
      

  10.   


    你自己看,你说麻烦不麻烦,呵呵select ID, Name  from stu where ID = 2
    union all
    select ID, Name  from stu where ID = 3
    union all
    select ID, Name  from stu where ID = 1
      

  11.   

    OK!这样吧:
    select ID, Name  from Protocol where ID = 2 union all
    select ID, Name  from Protocol where ID = 3  union all
    select ID, Name  from Protocol where ID = 1但又不确定In里有几个参数的,
    哎,还要判断数量循环。