android sqlite orderby datetime总是取第一行

时间:2014-03-19 14:06:20

标签: android sqlite

我将日期和时间存储在DATETIME类型的列中。在那之后,我试图找回最早的时间。 我正在使用此查询

String query_to_fetch_earliest="select *  from "+TABLE_NAME+" order  by datetime("+KEY_DATETIME+") DESC LIMIT 1";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query_to_fetch_earliest, null); 

但我总是最终获得第一排。 请帮忙。

2 个答案:

答案 0 :(得分:3)

不要设置限制,只需按以下步骤

  String query_to_fetch_earliest="select *  from "+TABLE_NAME+" order  by         datetime("+KEY_DATETIME+") DESC ";
  SQLiteDatabase db = this.getWritableDatabase();
  Cursor cursor = db.rawQuery(query_to_fetch_earliest, null); 

答案 1 :(得分:0)

试试这个:

String query = "SELECT * FROM " +
  TABLE_NAME +
  " WHERE " + 
  KEY_DATETIME +
  " IN (SELECT max(" +
  KEY_DATETIME +
  ") FROM " +
  TABLE_NAME +
  ")"