有时需要select id in 提取数据,但是提取的数据通常都是自带了ASC排序的,如何使用默认的in中的排序呢?经过检索,找到了解决办法,示例代码如下:
MySQL写法:
SELECT * FROM EVENT WHERE eventId IN(443,419,431,440,420,414,509) ORDER BY INSTR(',443,419,431,440,420,414,509,',CONCAT(',',eventId,','))
Oracle写法:
select name from order where oderid in(111,222,333,444,555,666)order by instr('111,222,333,444,555,666',orderid)
方法来自:http://blog.csdn.net/liyanhui1001/article/details/8508192
还有其他实现方式:
select id from article where id in(1,2,3) order by field(id,3,1,2);
方法来自:李伟的博客-mysql查询结果自定义排序
未经允许不得转载:阿藏博客 » MySQL 按 select in 的id顺序排序