在android sqlite中选择不同的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11219361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:22:15  来源:igfitidea点击:

select distinct value in android sqlite

androidsqlite

提问by smith

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

我尝试在 android 中运行以下原始查询,它似乎不起作用

String query ="SELECT DISTINCT category FROM event";
Cursor  cursor = mDb.rawQuery(query, null);
if (cursor != null) {
     cursor.moveToFirst();
}
return cursor; 

so I decide to use the query() method in android which are something like

所以我决定在 android 中使用 query() 方法,类似于

Cursor mCursor = mDb.query(EVENT_TABLE, new String[] {KEY_ROWID, KEY_CAT}, null, null,null,null, null)

Can anyone show me how to select the distinct category for using query() instead of rawquery please, any help will be greatly appreciated!

谁能告诉我如何选择使用 query() 而不是 rawquery 的不同类别,任何帮助将不胜感激!

回答by Adam Kowalski

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

但是你必须记住在GROUPBY( NOT NULLsend) 中发送参数。

You must give column name for distinct.

您必须为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.

COLUMN_NAME_2- 将列命名为 DISTINCT。

That's works for me fine.

这对我有用。

回答by Harry Joy

You can use thismethod:

您可以使用方法:

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.

这里的第一个参数指定是否使用 distinct 。

回答by Pavan Kunchapu

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

回答by Ngosagoogle

Cursor res=db.rawQuery("select column1 column2...  Distinct column3 from "+Table_name, null);

column3is a distinct column

column3是一个不同的列