select distinct value in android sqlite

前端 未结 4 1305
心在旅途
心在旅途 2020-11-30 05:24

I try to run the following raw query in android, it seems not work

String query =\"SELECT DISTINCT category FROM event\";
Cursor  cursor = mDb.rawQuery(query         


        
相关标签:
4条回答
  • 2020-11-30 06:02

    There are multiple ways, as already green ticked. But if somebody is looking to get it via raw query then here you go:

    String query = "SELECT DISTINCT(category) FROM event";
    
    0 讨论(0)
  • 2020-11-30 06:07
    Cursor res=db.rawQuery("select column1 column2...  Distinct column3 from "+Table_name, null);
    

    column3 is a distinct column

    0 讨论(0)
  • 2020-11-30 06:11

    You can use this method:

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

    Here first argument specifies whether to use distinct or not.

    0 讨论(0)
  • 2020-11-30 06:25

    But you MUST remember to send argument in GROUPBY (NOT NULL send).

    You must give column name for distinct.

    Example:

    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);
    

    true - distinct TRUE

    COLUMN_NAME_2 - name column what you have be DISTINCT.

    That's works for me fine.

    0 讨论(0)
提交回复
热议问题