場(chǎng)景:
之前在項(xiàng)目中遇到這樣的問(wèn)題,今天又遇到了,查了一些資料,記錄一下
我在查詢數(shù)據(jù)中,按照某個(gè)狀態(tài)排序的時(shí)候,我的狀態(tài)實(shí)際值是:0,1,2,3,4,5 而我項(xiàng)目需求是按 2,3,5,4,1 0排序;
問(wèn)題出現(xiàn)了,無(wú)論是正序還是逆序 都不能滿足我當(dāng)前的需求,怎么辦?
解決方案:
方法1:直接sql 更改狀態(tài)值為想要的順序.
order by select status case when 2 then 5 case when 2 then 4? case when 5 then 3 case when 4 then 2 case 1 case else 0 order by status asc;
方法2:利用where in 方法?
具體sql如下 select * from table where status in (2,3,5,4,1 0) order by find_in_set(id,'2,3,5,4,1 0');