select id,name from table1 where id=2
union all
select id,name from table1 where id=1
union all
select id,name from table1 where id=4
union all
select id,name from table1 where id=3

解决方案 »

  1.   

    是不是这样:
    先select id ,name from t where id in (2,1,4,3)得到结果集:id             name
    1                q
    2                r
    3                3
    4                5怎么样才能按2,1,4,3的顺序插到临时表中?
      

  2.   

    先建一个辅助表
    create table t1(id int)
    里面你需要的顺序插入,比如2,1,4,3insert into t1
    select 2
    union all select 1
    union all select 4
    union all select 3declare @id int
    declare c_test cursor for
    select id from t1                        --定义游标select * into #tmp from table1 where 1=2 --构造临时表的结构OPEN c_test FETCH NEXT FROM c_test 
    INTO @id
    WHILE @@FETCH_STATUS = 0
    BEGIN
    insert into #tmp select id,name from table1 where id=@id   --按t1中的id顺序插数据到临时表
    FETCH NEXT FROM c_test  INTO @id
    End
    Close c_test                   
    deallocate c_testselect * from #tmp                   --从临时表中获得所需要的数据
      

  3.   

    测试结果select * from #tmpid          name 
    ----------- ---- 
    2           r
    1           q
    4           5
    3           3(所影响的行数为 4 行)
      

  4.   

    太感谢宁哥了,辅助表t1是不是也应该建成临时表?还有如果是逗号分隔的字符串应该可以用charIndex和subString就可以取得两个逗号之间的值了,这样就不用辅助表和游标了
      

  5.   

    如何將下面這樣的一個字符串拆分成獨立的多個字符串
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=2817
      

  6.   

    好了,确实不需要用游标,给你个完美版,哈哈
    select * into #tmp from table1 where 1=2 --构造临时表的结构declare  @str  varchar(300),@id  varchar(300),@m  int,@n  int  
    set  @str='2,1,4,3,'      ---注意后面有个逗号
    set  @m=CHARINDEX(',',@str)  
    set  @n=1  
    WHILE  @m>0  
    BEGIN  
           set  @id=substring(@str,@n,@m-@n)  
           print  @id  
           insert into #tmp select id,name from table1 where id=convert(int,@id)
           set  @n=@m+1  
           set  @m=CHARINDEX(',',@str,@n)  
    END  select * from #tmp
      

  7.   

    select id ,name from t where id in (2,1,4,3) 
    order by (case id when 2 then 'A' when 1 then 'B' when 4 then 'C' when 3 then 'D' end)select id ,name from t where id in  (1,4,2,3)
    order by (case id when 1 then 'A' when 4 then 'B' when 2 then 'C' when 3 then 'D' end)
      

  8.   

    --不用什么临时表或者游标,这样写就可以了!select * from aaa
    order by case name when 'r' then 1
                       when 'q' then 2
                       when '5' then 3
                       when '3' then 4
              end--测试结果
    /*
    id          name       
    ----------- ---------- 
    2           r
    1           q
    4           5
    3           3(所影响的行数为 4 行)
    */
      

  9.   

    楼上的两位方法不错,又偷学了一招,之前还真不知道order by里也可以用case.不过看楼主的意思恐怕有至少超过256条,一条条写也不是办法.还是用字符串分割循环插入临时表的方法有通用性些,呵呵
      

  10.   

    我把这个帖子总结到我的blog了,还有新方法的欢迎提出啊,呵呵http://blog.csdn.net/ningoo/archive/2005/01/01/236734.aspx
      

  11.   

    下午去逛街,晚上回来结贴,感谢上面的几位热心朋友们,祝大家2005年好运连连,万事如意哦
    宁哥辛苦了,还专门找出用charIndex和subString分隔字符串的方法,其实这个小弟自己也会写的,呵呵,再次感谢!我以前没想到一个一个选出来插到临时表里面,老是想怎么能排序,钻牛角尖了以后有问题还要请大家指点
      

  12.   

    select * from t_test order by case id when 2 then 0 when 1 then 1 when 4 then 2 when 3 then 4 end