Reputation: 338
I'm now working Realm with java, I have no idea how can I use subquery.
I have two RealmObjects like below.
public class Sms extends RealmObject {
@PrimaryKey
private String id;
private String address;
}
public class Statement extends RealmObject {
@PrimaryKey
private String id;
private String smsId;
}
And I want query with Realm to do same work like sql below.
SELECT
*
FROM
sms
WHERE
id not in (
SELECT
sms_id
FROM
statement
)
and address = "123456789"
;
How can I achieve it?
Update 2015-12-18 :
@geisshirt, many thanks for answer kindly.
I had a idea from his answer, and below is result.
RealmResults<Statement> statements = realm.where(Statement.class).findAll();
RealmQuery<Sms> query = realm.where(Sms.class).equalTo("address", "123456789");
for(Statement statement : statements){
query = query.notEqualTo("id", statement.getSmsId());
}
RealmResults<Sms> smsList = query.findAll();
I think this will achieve my goal, but I'm doubtful about this is the only way to achieve not in
or in
statement.
Is there noway to do this more clearly?
Upvotes: 3
Views: 1563
Reputation: 2497
With your current model, you will have to iterate:
RealmResults<Statement> statements = realm.where(Statement.class).equalTo("address", "123456789").findAll();
for(Statement statement : statements) {
RealmResults<Sms> sms = realm.where(Sms.class).notEqualTo("id", statement.smsId).findAll();
// do something to sms
}
I'm not sure what the query really means: did you mean id in
instead of id not in
?
Upvotes: 1