oracle in 按原顺序输出 - order by decode
原创    ms_eye    发表于:2018-01-31 14:20:21
  阅读 :140   收藏   编辑

创建测试表;

create table ts_order (no varchar(20));

模拟数据:

insert into ts_order values('20171227001');
insert into ts_order values('20171227002');
insert into ts_order values('20171227003');
insert into ts_order values('20171227004');
insert into ts_order values('20171227005');

in查询:

select * from ts_order where no in (

'20171227004',
'20171227002',
'20171227005',
'20171227003',
'20171227001'
)

如何在输出的时候,也是按照输入in里的前后顺序,进行输出。
这里给大家介绍oracle decode 函数

sql如下:

select * from ts_order where no in (

'20171227004',
'20171227002',
'20171227005',
'20171227003',
'20171227001'

) order by decode(no, 
'20171227004', 1,
'20171227002', 2,
'20171227005', 3,
'20171227003', 4,
'20171227001', 5
);

只要保证decode no字段,按从小到大的顺序,也就是上述的1,2,3,4,5即可

评论
条评论