2014-04-03 27 views
0

這是我的方法,該方法返回MAX(_id)的Android SQLite的最大回報127

public static int getMaxId(Context context, Uri uri, String columnName) { 
     //it is all  
     String []projection = new String[]{"MAX("+ columnName +")"};  
     Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);  
     cursor.moveToFirst();  
     return cursor.getInt(0); 
    } 

如果在數據庫中的記錄數<127一切正常,但如果記錄>127數(例如250或500),該方法返回127

集合函數max()返回組中所有值的最大值。最大值是最後在同一列上的ORDER BY中返回的值。當且僅當該組中沒有non-NULL值時,聚合max()返回NULL

回答

0

如果你有一個這樣的專欄,說將其命名爲「myorder」,那麼你可以使用一個 這樣的查詢來獲取最後一行:

select * from mytable 
    where myorder IN (select max(myorder) from mytable) 

如果你的意思是讓這將排序行最後在查詢中,那麼你有 像這樣在查詢結束後,「ORDER BY」之後:

LIMIT 1 OFFSET (select count(*) from mytable) - 1 

here