如果 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'排在第七位.
如: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'排在第七位.
是instr,以前某个高人写的
得出来的表的顺序是按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 ||',' )
先匹配的数字小就排在前面了
完整
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