天天看點

根據特定順序查詢進行排序查詢

根據特定順序查詢

有這麼一個需求, 根據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) ;      

查詢完成:

繼續閱讀