Realm.io 使用 GroupBy 查询
Realm.io Query with GroupBy
我想按月对一些帐户进行分组,我可以使用 Realm.io 来实现吗?
public class Account extends RealmObject {
.....
private Date date;
}
RealmResults accounts = realm.where(Account.class)
.beginGroup()
.equalTo("date", "MONTH(date)")//<----- wrong code
.endGroup()
.findAll();
谢谢
Realm 尚不支持 GroupBy。还要注意 beginGroup() 实际上与括号相同。所以你的查询实际上被解释为:
// SQL pseudo code
SELECT * FROM Account WHERE (date = MONTH(date))
在 Realm 中,您必须在一个月内 select 做这样的事情:
// Between is [ monthStart, monthEnd ]
Date monthStart = new GregorianCalendar(2015, 5, 1).getTime();
Date monthEnd = new GregorianCalendar(2015, 6, 1).getTime() - 1;
accounts = realm.where(Account.class).between("date", monthStart, monthEnd).findAll();
或类似的东西来检测月份何时变化
// pseudo code. You might want to use Calendar instead
accounts = realm.where(Account.class).findAllSorted("date")
Iterator<Account> it = accounts.iterator();
int previousMonth = it.next().getDate().getMonth();
while (it.hasNext) {
int month = it.next().getDate().getMonth();
if (month != previousMonth) {
// month changed
}
previousMonth = month;
}
我想按月对一些帐户进行分组,我可以使用 Realm.io 来实现吗?
public class Account extends RealmObject {
.....
private Date date;
}
RealmResults accounts = realm.where(Account.class)
.beginGroup()
.equalTo("date", "MONTH(date)")//<----- wrong code
.endGroup()
.findAll();
谢谢
Realm 尚不支持 GroupBy。还要注意 beginGroup() 实际上与括号相同。所以你的查询实际上被解释为:
// SQL pseudo code
SELECT * FROM Account WHERE (date = MONTH(date))
在 Realm 中,您必须在一个月内 select 做这样的事情:
// Between is [ monthStart, monthEnd ]
Date monthStart = new GregorianCalendar(2015, 5, 1).getTime();
Date monthEnd = new GregorianCalendar(2015, 6, 1).getTime() - 1;
accounts = realm.where(Account.class).between("date", monthStart, monthEnd).findAll();
或类似的东西来检测月份何时变化
// pseudo code. You might want to use Calendar instead
accounts = realm.where(Account.class).findAllSorted("date")
Iterator<Account> it = accounts.iterator();
int previousMonth = it.next().getDate().getMonth();
while (it.hasNext) {
int month = it.next().getDate().getMonth();
if (month != previousMonth) {
// month changed
}
previousMonth = month;
}