Alejandro Diego
Alejandro Diego

Reputation: 23

Nesting and / or conditions with Parse.com

this is my first post here. I'm taking my first steps with Parse.com and now I am stuck with a problem I have not been able to solve. Previous to asking I have been searching for a solution but I have found nothing.

The fact is that I have to retrieve the records from a table according to the values of two different columns.

Let's consider the following table:

idSection   idItem
---------   ------
1             10 
1             11
1             12
2             20
2             21
2             22
2             23
3             31
3             32
3             33

What I need is to create a select of 'and' / 'or' conditions in the following way:

select *
from table1
where (idSection = 1 and idItem > 11)
   or (idSection = 2 and idItem > 22)
   or (idSection = 3 and idItem > 32)

Any ideas?. Thanks in advance.

Upvotes: 2

Views: 392

Answers (1)

chasew
chasew

Reputation: 8828

If you are in iOS, you can do this:

NSPredicate * pred = [NSPredicate predicateWithFormat:@"(idSection = 1 and idItem > 11) or (idSection = 2 and idItem > 22) or (idSection = 3 and idItem > 32)"];
PFQuery * q = [PFQuery queryWithClassName:@"MyClass" predicate:pred];
[q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    NSLog(@"OBJ : %@",objects);
}];

If javascript, do this:

var clz = "MyClass";

var q1 = new Parse.Query(clz);
q1.equalTo("idSection", 1);
q1.greaterThan("idItem", 11);

var q2 = new Parse.Query(clz);
q2.equalTo("idSection", 2);
q2.greaterThan("idItem", 22);

var q3 = new Parse.Query(clz);
q3.equalTo("idSection", 3);
q3.greaterThan("idItem", 32);

var mainQuery = Parse.Query.or(q1,q2,q3);
mainQuery.find({
  success: function(results) {

  },
  error: function(error) {

  }
});

Android is similar to javascript.. see the "or" method in ParseQuery class here https://parse.com/docs/android/api/

Upvotes: 2

Related Questions