根据特定顺序排序Sqlite

时间:2014-05-08 06:07:45

标签: ios sql objective-c sqlite android-sqlite

我有一个表,需要对两列进行排序,第一列是varchar列,sencond是日期列...

问题是......对于第一列我不想根据字母顺序进行排序

表格结构是

 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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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];