解析查询 - 将OR语句与AND组合

时间:2014-11-30 06:20:19

标签: ios objective-c parse-platform

是否有人知道如何在Parse for iOS中将OR语句与AND组合?所以我基本上想做类似这样的SQL语句:

select * from hotels where (city = 'New York' OR city = 'Chicago') AND (price < 1000 OR price > 2500)

有许多文档用于使用orQueryWithSubqueries方法在Parse中组合一系列OR子句,但是我一直无法找到任何可靠的组合OR语句和AND运算符的方法。

2 个答案:

答案 0 :(得分:2)

我相信这种查询对Parse来说有点痛苦,因为你需要扩展OR查询,因为每个OR查询都需要完成。我认为下面的内容可能就是你所追求的。 (我也在这里猜你的班级名字)。

PFQuery *cheapNYQuery = [PFQuery queryWithClassName:@"Hotels"];
[cheapNYQuery whereKey:@"city" equalTo:"New York"];
[cheapNYQuery whereKey:@"price" lessThan:@1000];
PFQuery *expensiveNYQuery = [PFQuery queryWithClassName:@"Hotels"];
[expensiveNYQuery whereKey:@"city" equalTo:"New York"];
[expensiveNYQuery whereKey:@"price" greaterThan:@2500];
PFQuery *cheapCHQuery = [PFQuery queryWithClassName:@"Hotels"];
[cheapCHQuery whereKey:@"city" equalTo:"Chicago"];
[cheapCHQuery whereKey:@"price" lessThan:@1000];
PFQuery *expensiveCHQuery = [PFQuery queryWithClassName:@"Hotels"];
[expensiveCHQuery whereKey:@"city" equalTo:"Chicago"];
[expensiveCHQuery whereKey:@"price" greaterThan:@2500];
PFQuery *hotelQuery = [PFQuery orQueryWithSubqueries:@[cheapNYQuery, expensiveNYQuery, cheapCHQuery, expensiveCHQuery]];
// Run hotelQuery.

您可以通过使用酒店和价格的输入数组使上述更通用/可重复使用,并循环生成OR的查询,并将它们添加到可变数组中。

答案 1 :(得分:2)

我相信您可以使用NSPredicate

更轻松
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city = 'New York' OR city = 'Chicago' AND price < @1000 OR price > @2500"];
PFQuery *query = [PFQuery queryWithClassName:@"Hotel" predicate:predicate];

[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {

}];