簡體   English   中英

如何從sql數據庫中獲取唯一值

[英]how to get unique values from sql database

以下代碼提供了數據庫中的所有值,甚至是重復值。

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

如何從上面的代碼中獲取不同的值

使用以布爾值作為第一個參數query重載,並從select中刪除_id列:

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

_id列應該是唯一的,因此在查詢中包含它將導致不同的效果。

使用distinct關鍵字,這將返回數據庫中的唯一記錄。

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

使用此查詢:

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

將第一個參數設置為true,並且不要在選擇中使用primare鍵。 它永遠是獨一無二的......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM