简体   繁体   English

如何从sql数据库中获取唯一值

[英]how to get unique values from sql database

The following code gives all values from database even duplicate values. 以下代码提供了数据库中的所有值,甚至是重复值。

return database.query("contacts", new String[] {"_id", "name"}, 
     null, null, null, null, "name");

how can i get distinct values from above code 如何从上面的代码中获取不同的值

Use the overload of query that takes a boolean as first parameter and remove the _id column from the select: 使用以布尔值作为第一个参数query重载,并从select中删除_id列:

return database.query(true, "contacts", new String[] {"name"}, 
                      null, null, null, null, "name", null);

The _id column is supposed to be unique, so including it in your query will lead to the distinct having no effect. _id列应该是唯一的,因此在查询中包含它将导致不同的效果。

使用distinct关键字,这将返回数据库中的唯一记录。

db.rawQuery("Select DISTINCT from table_name",null);

use this query: 使用此查询:

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

set the first param to true and do not use youre primarey key in selection. 将第一个参数设置为true,并且不要在选择中使用primare键。 it will be always unique... 它永远是独一无二的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM