根据特定顺序查询
有这么一个需求, 根据test_user.status进行查询, 并且按2,1,0,4,3 进行查询.
select id,`status`,name from test_user order by field(`status`, 2, 1, 0, 4, 3);
查询完成:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5iNzAjN1YjYyAzNwEGNjVDOyYzXyMjNxITM5EzLcVDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
哦, 灾难来了, status居然有null, 那么null 放到最后应该怎么做呢?
select `id`,`name`, `status` from test_user order by `status` is null, field(`status`, 2,1,0,4,3);
查询得到了我们想要的结果:
那么好玩的来了, 将null也参与到排序中, 完成2,1,0,null,4,3 的排序应该怎么做呢?
select `id`,`name`, `status` from test_user order by field(`status`,4,3), `status` is null, field(`status`, 2,1,0) ;
查询完成: