34

DISTINCT添加和/或基于GROUPBY-ContentResolver的查询的正确方法是什么?

现在我必须为每个特殊情况创建自定义 URI。

有没有更好的办法?

(我仍然将 1.5 编程为最低公分母)

4

11 回答 11

43

查询 contentResolver 时,您可以做很好的 hack,使用:

String selection = Models.SOMETHING + "=" + something + ") GROUP BY (" + Models.TYPE;
于 2010-12-17T11:59:54.790 回答
19

如果您想将 DISTINCT 与多于一列的 SELECT 一起使用,则需要使用 GROUP BY。
使用 ContentResolver.query 的 Mini Hack 使用:

Uri uri = Uri.parse("content://sms/inbox");
        Cursor c = getContentResolver().query(uri, 
            new String[]{"DISTINCT address","body"}, //DISTINCT
            "address IS NOT NULL) GROUP BY (address", //GROUP BY
            null, null);
        if(c.moveToFirst()){
            do{
                Log.v("from", "\""+c.getString(c.getColumnIndex("address"))+"\"");
                Log.v("text", "\""+c.getString(c.getColumnIndex("body"))+"\"");

            } while(c.moveToNext());
        }

此代码从设备收件箱中为每个发件人选择最后一条短信。
注意:在 GROUP BY 之前,我们总是需要写至少一个条件。ContentResolver.query 方法中的结果 SQL 查询字符串将:

SELECT DISTINCT address, body FROM sms WHERE (type=1) AND (address IS NOT NULL) GROUP BY (address) 
于 2013-04-30T13:37:25.173 回答
15

由于没有人来回答,我只想告诉我如何解决这个问题。基本上我会为每种情况创建自定义 URI 并在selection参数中传递标准。然后在里面ContentProvider#query我会识别案例并根据表名和选择参数构造原始查询。

这是一个简单的例子:

switch (URI_MATCHER.match(uri)) {
    case TYPES:
        table = TYPES_TABLE;
        break;
    case TYPES_DISTINCT:
        return db.rawQuery("SELECT DISTINCT type FROM types", null);
    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
    return db.query(table, null, selection, selectionArgs, null, null, null);
于 2010-02-27T20:13:02.127 回答
14

在您覆盖的ContentProvider查询方法中,有一个特定的 URI 映射到使用 distinct。

然后使用SQLiteQueryBuilder并调用该setDistinct(boolean)方法。

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder)
{
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    boolean useDistinct = false;

    switch (sUriMatcher.match(uri))
    {
    case YOUR_URI_DISTINCT:
        useDistinct = true;
    case YOUR_URI:
        qb.setTables(YOUR_TABLE_NAME);
        qb.setProjectionMap(sYourProjectionMap);
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    // If no sort order is specified use the default
    String orderBy;
    if (TextUtils.isEmpty(sortOrder))
    {
        orderBy = DEFAULT_SORT_ORDER;
    }
    else
    {
        orderBy = sortOrder;
    }
    // Get the database and run the query
    SQLiteDatabase db = mDBHelper.getReadableDatabase();
            // THIS IS THE IMPORTANT PART!
    qb.setDistinct(useDistinct);
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

    if (c != null)
    {
        // Tell the cursor what uri to watch, so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
    }

    return c;
}
于 2010-12-20T20:02:06.043 回答
7

虽然我没有使用Group By,但我在内容解析器查询中使用了Distinct 。

Cursor cursor = contentResolver .query(YOUR_URI, new String[] {"Distinct "+ YOUR_COLUMN_NAME}, null, null, null);

于 2015-08-20T05:37:37.963 回答
1

在投影中添加 Distinct 关键字也对我有用,但是,它仅在 distinct 关键字是第一个参数时才有效:

String[] projection = new String[]{"DISTINCT " + DBConstants.COLUMN_UUID, ... };
于 2017-02-11T14:58:29.863 回答
0

在某些情况下,我们可以使用“distinct(COLUMN_NAME)”作为选择,并且效果很好。但在某些情况下,它会导致异常。

当它导致异常时,我将使用 HashSet 来存储列值....

于 2012-05-07T06:39:36.377 回答
0
// getting sender list from messages into spinner View
    Spinner phoneListView = (Spinner) findViewById(R.id.phone_list);
    Uri uri = Uri.parse("content://sms/inbox");     
    Cursor c = getContentResolver().query(uri, new String[]{"Distinct address"}, null, null, null);
    List <String> list;
    list= new ArrayList<String>();
    list.clear();
    int msgCount=c.getCount();
    if(c.moveToFirst()) {
        for(int ii=0; ii < msgCount; ii++) {
            list.add(c.getString(c.getColumnIndexOrThrow("address")).toString());
            c.moveToNext();
        }
    }
    phoneListView.setAdapter(new ArrayAdapter<String>(BankActivity.this, android.R.layout.simple_dropdown_item_1line, list));
于 2016-02-09T15:21:28.387 回答
0

我创建了一个使用 group by 和 distinct 的实用方法。

用法

thread_id这是从 MMS 数据库中选择最后一条消息日期的 unseen 示例。

query(contentResolver= contentResolver,
        select = arrayOf(Mms.THREAD_ID, "max(${Mms.DATE}) as date"),
        from = Mms.CONTENT_URI,
        where = "${Mms.SEEN} = 0",
        groupBy = "1",
        orderBy = "2 desc"
        ).use {
    while (it?.moveToNext() == true){
        val threadId = it.getInt(0)
        val date = it.getLong(1)
    }
}

资源

fun query(
        contentResolver: ContentResolver,
        from: Uri,
        select: Array<String>,
        where: String? = null,
        groupBy: Array<out String>? = null,
        distinct: Boolean = false,
        selectionArgs: Array<out String>? = null,
        orderBy: String? = null,
): Cursor? {
    val tmpSelect = select[0]
    val localWhere =
            if (groupBy == null) where
            else "${where ?: "1"}) group by (${groupBy.joinToString()}"
    if (distinct) {
        select[0] = "distinct $tmpSelect"
    }
    val query = contentResolver.query(from, select, localWhere, selectionArgs, orderBy)
    select[0] = tmpSelect
    return query
}
于 2021-04-01T16:58:21.460 回答
0

当您的投影中有多个列时,您应该这样做:

    val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val projection = arrayOf(
        "DISTINCT " + MediaStore.Images.Media.BUCKET_ID,
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.Images.Media.BUCKET_ID,
        MediaStore.MediaColumns.DATA
    )
    val groupBySelection = " 1) GROUP BY (${MediaStore.Images.Media.BUCKET_ID}"
    contentResolver.query(
        uri,
        projection,
        null,
        groupBySelection,
        null,
        null
    )

带有右括号和数字“1”的 groupBySelection 是一个小技巧,但它工作得非常好

于 2020-04-15T16:08:08.443 回答
-1

获得不同的值可能更简单,尝试在您想要的列名之前添加 DISTINCT 单词到投影表中

String[] projection = new String[]{
                BaseColumns._ID,
                "DISTINCT "+ Mediastore.anything.you.want
};

并将其用作内容解析器的查询方法的参数!

我希望对您有所帮助,因为几天前我也有同样的问题

于 2011-06-28T12:51:11.480 回答