2014-02-13 39 views
1

我想按字符串列進行排序,其中列是枚舉。例如:如何通過Oracle中的枚舉排序DB

+----+--------+----------------------+ 
| ID | NAME |  STATUS  | 
+----+--------+----------------------+ 
| 1 | Serdar | ACTIVE    | 
| 2 | John | DEACTIVE    | 
| 3 | Jerry | WAITING_FOR_APPROVAL | 
| 4 | Jessie | REJECTED    | 
+----+--------+----------------------+ 

我想訂購STATUS。它應該對結果進行排序,使得第一個結果必須有STATUS = WAITING_FOR_APPROVAL,然後是ACTIVE,然後是DEACTIVE,然後是REJECTED。

有沒有辦法在SQL中做到這一點?在java中是否有類似Comparator的東西?

回答

4

您可以通過

SELECT id, name, status 
    FROM your_table 
ORDER BY (CASE status 
       WHEN 'WAITING_FOR_APPROVAL' THEN 1 
       WHEN 'ACTIVE' THEN 2 
       WHEN 'DEACTIVE' THEN 3 
       WHEN 'REJECTED' THEN 4 
       ELSE 5 
      END) 
+0

哦枚舉在CASE聲明和秩序的價值觀,這看起來很完美,我會嘗試這個明天:) – sedran

+1

我可以用Hibernate指標分析實現這個? – sedran