天天看点

根据特定顺序查询进行排序查询

根据特定顺序查询

有这么一个需求, 根据test_user.status进行查询, 并且按2,1,0,4,3 进行查询.

select id,`status`,name from test_user order by field(`status`, 2, 1, 0, 4, 3);      

查询完成:

根据特定顺序查询进行排序查询

哦, 灾难来了, 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) ;      

查询完成:

继续阅读