简体   繁体   English

从SQLite表查询记录并将它们分组

[英]Querying records and making them groups from SQLite Table

I am not really understanding how can this be done in single query. 我不太了解如何在单个查询中完成此操作。

PROBLEM: 问题:

I have a table like this 我有这样的桌子

id| phonenumber| message| group_name| datetime 

Example data 示例数据

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

What I want to do is query the above SqlLite table in such a way that I need to grab all the rows of particular phonenumber in Descending order of datetime . 我想做的就是以一种查询上面的SqlLite表的方式,使我需要以datetime降序获取特定电话号码的所有行。 So that I can put them in my HashMap with key as group_name and value as ArrayList of messages . 所以,我可以把它们放在我的HashMap与键group_name和值的ArrayList的messages

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

I am using GreenDao Library to fetch data from SqlLite, I tried like below 我正在使用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();

I managed to do above query using GROUP BY but it is not listing all rows.Do I have to get All data of particular number and separate it looping through each row based on group_name ? 我设法使用GROUP BY进行了上述查询,但没有列出所有行。我是否必须获取特定编号的所有数据并根据group_name遍历每一行将其分开? or is there any better way to do? 还是有更好的方法吗?

The SQL is quite simple, just a WHERE clause and an ORDER BY . SQL非常简单,只是一个WHERE子句和一个ORDER BY

The SQL looks like this: SQL看起来像这样:

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

I am not sure how this translates into your SQL generator, but it does involve removing the GROUP BY . 我不确定这如何转换为您的SQL生成器,但确实涉及删除GROUP BY

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

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