2014-05-08 57 views
1

我有一個表,並需要將其排序的兩列,第一個是VARCHAR列和sencond是一個日期列...排序基於特定的順序sqlite的

問題是...第一列我不想排序基於字母順序

表是意象結構

id  name  status  date 
--------------------------------------- 

1  aaaa  completed  '2014-05-02' 
2  aaaa  Running  '2015-05-03' 
3  aaaa  new   '2015-05-03' 
4  aaaa  Pause   '2015-05-04' 
5  aaaa  completed  '2015-05-08' 

我想狀態進行排序這樣

id  name  status  date 
--------------------------------------- 

    1  aaaa  Running  '2014-05-03' 
    2  aaaa  Pause  '2014-05-04' 
    3  aaaa  new   '2014-05-03' 
    4  aaaa  completed '2014-05-02' 
    5  aaaa  completed '2014-05-08' 

但排序這樣

我想狀態進行排序這樣

id  name  status  date 
--------------------------------------- 

    1  aaaa  Running  '2014-05-03' 
    2  aaaa  Pause  '2014-05-04' 
    3  aaaa  new   '2014-05-03' 
    4  aaaa  completed '2014-05-02' 
    5  aaaa  completed '2014-05-05' 

任何建議表示讚賞...

謝謝

回答

2

試試這個... 。

SELECT * FROM table 
ORDER BY 
(CASE WHEN status = 'Running' THEN 1 
WHEN status = 'Pause' THEN 2 
WHEN status = 'New' THEN 3 
WHEN status = 'Completed' THEN 4 
WHEN status = 'Deleted' THEN 5 END), Date ASC 
2

試試這個..

在Android中

String selectQuery = "SELECT * FROM " 
       + TABLE_CONTACTS 
       + " where isConvertToInvoice = 0 and loginUser_id ='" 
       + logInID 
       + "' ORDER BY CASE WHEN status = 'Running' THEN 1 WHEN status = 'Pause' THEN 2 WHEN status = 'new' THEN 3 WHEN status = 'Completed' THEN 4 END "; 

在IOS

NSString *query=[NSString stringWithFormat:@"SELECT * FROM TABLE_CONTACTS where isConvertToInvoice = 0 and User_id ='%@' ORDER BY CASE WHEN status = 'Running' THEN 1 WHEN status = 'Pause' THEN 2 WHEN status = 'new' THEN 3 WHEN status = 'Completed' THEN 4 END",logInID];