【转】mysql查询时,查询结果按where in数组排序

使用情况:当我们在进行where id in () 查询的时候,如果where in的id查询数组为[2,7,1,4,3],正常情况查询出来的结果顺序为[1,2,3,4,7],这可能不是我们想要的结果,我们期望查出来的结果顺序与where in的顺序一致,这里介绍两个方式:

1.使用find_in_set函数:

select * from table where id in (2,7,1,4,3) order by find_in_set(id,'2,7,1,4,3');
 

2.使用order by field

select * from table where id in (2,7,1,4,3) order by field(id,2,7,1,4,3);

 

from :https://blog.csdn.net/qq_33980878/article/details/82660216

posted on 2020-11-17 15:24  神奇的旋风  阅读(334)  评论(0编辑  收藏  举报

导航