Java 如何从sql数据库中获取唯一值
下面的代码给出了数据库中的所有值,甚至是重复的值Java 如何从sql数据库中获取唯一值,java,android,sql,Java,Android,Sql,下面的代码给出了数据库中的所有值,甚至是重复的值 return database.query("contacts", new String[] {"_id", "name"}, null, null, null, null, "name"); 如何从上述代码中获取不同的值使用以布尔值作为第一个参数的,并从选择中删除\u id列: return database.query(true, "contacts", new String[] {"name"},
return database.query("contacts", new String[] {"_id", "name"},
null, null, null, null, "name");
如何从上述代码中获取不同的值使用以布尔值作为第一个参数的,并从选择中删除\u id
列:
return database.query(true, "contacts", new String[] {"name"},
null, null, null, null, "name", null);
\u id
列应该是唯一的,因此在查询中包含它将导致distinct无效。使用此查询:
public Cursor query (boolean distinct, String table,
String[] columns, String selection,
String[] selectionArgs, String groupBy,
String having, String orderBy, String limit)
将第一个参数设置为true,并且在选择中不要使用youre primarey键。它将始终是唯一的…使用distinct关键字这将从数据库返回唯一的记录
db.rawQuery("Select DISTINCT from table_name",null);
您可能应该使用
rawQuery
而不是query
,并使用带有DISTINCT
关键字的真正SQL查询来执行此操作。另请参见。@JohnWillemse:为什么不使用Stefan或我的答案所指示的query
重载?当然可以。因为您也选择了primarey键“_id”。这个id总是唯一的。。。