问题描述
我有一个指向内容的URL,我需要获得其中一列中包含的最高值.是否有任何汇总功能可以实现这一目标,或者我必须手动执行此操作?
推荐答案
如果您要查询Android内容提供商,则应该能够通过将MAX(COLUMN_NAME)传递到ContentResolver.query的选择参数来实现:
getContentResolver().query(uri, projection, "MAX(COLUMN_NAME)", null, sortOrder);
uri是内容提供商的地址.这应该在column_name中返回具有最高值的单行.
其他推荐答案
Android的数据库使用SQLITE,因此SELECT MAX(thecolumn) FROM TheTable应该像其他任何其他SQLITE实现一样工作(或为此,无论其他任何其他SQL," ITE"," ITE"是否;-). (如果您不使用android.database,则最好指定 您正在使用; - ).
其他推荐答案
对我有用.
基于@reto Meier和@florian von Stosch的响应.
public static long getMaxId(Context context) { long maxId = 0; Cursor maxCursor = context.getContentResolver().query( ProviderContentContract.CONTENT_URI, new String[]{"MAX(" + Table._ID + ")"}, null, null, null); if (maxCursor != null && maxCursor.moveToFirst()) { maxId = maxCursor.getInt(0); maxCursor.close(); } return maxId; }
问题描述
I have an URL pointing to content and I need to get the highest value contained in one of the columns. Is there any aggregate function that will accomplish that or do I have to do this manually?
推荐答案
If you're querying an Android content provider, you should be able to achieve this by passing MAX(COLUMN_NAME) in to the selection parameter of ContentResolver.query:
getContentResolver().query(uri, projection, "MAX(COLUMN_NAME)", null, sortOrder);
Where Uri is the address of the content provider. This should return the single row with the highest value in COLUMN_NAME.
其他推荐答案
Android's database uses SQLite, so SELECT MAX(thecolumn) FROM TheTable should work, just like in any other SQLite implementation (or for that matter any other SQL, "ite" or not;-). (If you're not using android.database you'd better specify what you're using instead;-).
其他推荐答案
That worked for me.
Based on the responses of @Reto Meier and @Florian von Stosch.
public static long getMaxId(Context context) { long maxId = 0; Cursor maxCursor = context.getContentResolver().query( ProviderContentContract.CONTENT_URI, new String[]{"MAX(" + Table._ID + ")"}, null, null, null); if (maxCursor != null && maxCursor.moveToFirst()) { maxId = maxCursor.getInt(0); maxCursor.close(); } return maxId; }