當前遇到一項目的活動排序需求是:
活動列表,排序要做如下修改:
1、首先根據活動狀態排序;進行中 => 未開始 => 已結束來排
2、已經結束的活動,要按“開始時間”的倒序來排
3、進行中,未開始的活動,要按“開始時間”的順序來排
因爲在開始是設計的活動狀態序號是:1、未開始,2、進行中,3、已結束,因此導致排序稍微變的複雜
以下是我詳細的排序實現:
select *
from act
order by
case when status=2 then 0 ELSE 4 END, // 先對進行中的狀態進行替換,然後按順序排
status ASC,
case when status = 1 then btime end, // 然後對相應狀態進行不同的排序處理
case when status = 2 then btime end,
case when status = 3 then btime end desc;