Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a cursor with distinct values only?

I want to return a cursor only with distinct values of a column. The column 'Groups' has more items but with only 2 values: 1,2,1,1,1,2,2,2,1

String[] FROM = {Groups,_ID};

public Cursor getGroups(){
//......
return db.query(TABLE_NAME,FROM,null,null,null,null,null);
}

will return a cursor containing {1,2,1,1,1,2,2,2,1} but I would like to contain just {1,2}.

like image 751
AlexAndro Avatar asked Dec 12 '22 06:12

AlexAndro


2 Answers

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);
    }
like image 114
Lalit Poptani Avatar answered Jan 07 '23 05:01

Lalit Poptani


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.

like image 34
Vineet Shukla Avatar answered Jan 07 '23 03:01

Vineet Shukla