如果 id集合中顺序是3,2,1,4(别人程序经过排序返回),我需要用它们的顺序,按顺序找出结果. 
如:t.content_id值的集合为('426B84D002','426B84D008'...)
 如何确保t.content_id ='426B84D002'记录排在第一位,
t.content_id ='426B84D008'的记录排在第二位
....  select *     
  from T_CONTENT t
where  t.content_id ='426B84D002D'
UNION ALL 
  select *     
  from T_CONTENT t
where  t.content_id ='426B84D008'
这种方法在集合值多时,不怎么好用.有没有其他的方法呢.
如图t.content_id ='426B84D002D14930E0440018FE2D3012'排在第七位.

解决方案 »

  1.   

    order by indexOf(','||'426B84D002','426B84D008'...||',',','||content_id||',')
      

  2.   

    order by indexOf(',' ¦ ¦'426B84D002','426B84D008'... ¦ ¦',',',' ¦ ¦content_id ¦ ¦','),不太明白,能说一下么?谢谢.
      

  3.   

    写错了
    是instr,以前某个高人写的
      

  4.   

    现有这样一条SQL语句:select * from question where qt_id in (26,1,2,12,19)
    得出来的表的顺序是按qt_id的从小到大顺序排的,可是我想得到的顺序和括号中的顺序一样(就是表中行按qt_id=26,1,2,12,19这样的顺序),请大家指教!sql-serverz中的写法:
    select * from question where qt_id in (26,1,2,12,19) order by CharIndex(',' + Cast(qt_id As Varchar) + ',', ',26,1,2,12,19,')
    oracle中的写法:
    select * from aa 
    where id in (2,3,1,10,-1) 
    order by instr( ',2,3,1,10,-1,',','|| id ||',' )
      

  5.   

    indexOf是java中的写法查看instr函数
    先匹配的数字小就排在前面了
      

  6.   

    HelloWorld_001 给的方法好,强烈支持
      

  7.   

    谢谢HelloWorld_001 给的方法好
    完整
    select t.*  
     mytable.pos
      from T_CONTENT t,
           (select temp.content_id,
                   instr(',' || '426B84D002D14930E0440018FE2D3012' ||',' || '426B84D008D54930E0440018FE2D3012' || ',',
                         ',' || temp.content_id || ',') pos
              from T_CONTENT temp
             where null is null
               and (temp.CONTENT_ID = '426B84D002D14930E0440018FE2D3012' or temp.CONTENT_ID = '426B84D008D54930E0440018FE2D3012' )) mytable
     where t.content_id = mytable.content_id
     order by pos