How to get a cursor with distinct values only?

*爱你&永不变心* 提交于 2019-11-29 14:34:10

You can have an sql query like this,

public Cursor usingDistinct(String column_name) {
        return db.rawQuery("select DISTINCT "+column_name+" from "+TBL_NAME, null);
    }

you can use distinct argument while making query like this:

public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

Follow this doc for more clearity.

You can use below example query, as you have to give column name for distinct

Cursor cursor = db.query(true, YOUR_TABLE_NAME, new String[] { COLUMN_NAME_1 ,COLUMN_NAME_2, COLUMN_NAME_3 }, null, null, COLUMN_NAME_2, null, null, null);

COLUMN_NAME_2 - name of the column for distinct.

remember to add GROUP BY column names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!