2016-11-13 71 views
0

我並不真正瞭解如何在單個查詢中完成此操作。查詢記錄並從SQLite中創建它們組表

問題:

我有這樣

id| phonenumber| message| group_name| datetime 

實例數據的表

1 | 987654321 | "hi" | G1 | xxxxxxx 
2 | 987654321 | "hi" | G1 | xxxxxxx 
1 | 987145678 | "hello" | G2 | xxxxxxx 

我想要做的就是查詢上述SqlLite表以這樣的方式,我需要以datetime的降序獲取特定音名的所有行。這樣我就可以將它們放在我的HashMap中,其密鑰爲group_name,值爲ArrayList的messages

HashMap<String, ArrayList<String>> mapper = new HashMap<>(); 

我使用GreenDao圖書館從SqlLite獲取數據,我試着像下面

List<activity> activities = activityDao.queryBuilder().where(new WhereCondition.StringCondition(com.ficean.android.ficean.db.activityDao.Properties.Contact_number.eq(phonenumber) + "GROUP BY group_name")).orderDesc(com.ficean.android.ficean.db.activityDao.Properties.Date_time).build().list(); 

我設法用GROUP BY上面做查詢,但它沒有列出所有rows.Do我得所有特定數據的數據,並根據group_name循環遍歷每行?還是有什麼更好的辦法呢?

+0

爲什麼你需要這個HashMap?看起來你只是想將所有的數據庫存儲在內存中...... – lovasoa

回答

0

SQL很簡單,只是一個WHERE子句和一個ORDER BY

的SQL是這樣的:

select t.* 
from t 
where t.phone = ? 
order by t.datetime desc; 

我不知道如何轉換成您的SQL生成,但它確實涉及移除GROUP BY

+0

如果我理解了查詢的權利,他希望構建一個帶有phonenumber作爲鍵和消息的hashmap。所以不需要一個地方的條件。我會簡單地這樣做:'從t order by t.datetime desc'中選擇t。*,然後在散列映射的正確位置順序插入消息。 – lovasoa