2

我使用此链接作为指南,但我可以弄清楚如何使用 AND 子句进行解析。它仅在示例代码中定义 OR 子句。

如果我要从 SQL 语句转换它,它看起来像这样......

SELECT * FROM RemittanceTable WHERE beneCode = 'code' AND remittanceAmount = 500.00

例如在这段代码中,我想在读取数据之前检查代码和数量是否正确。但它使用 OR 子句验证

PFQuery *beneCodeQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[beneCodeQuery whereKey:@"beneCode" containsString:_beneCodeText.text];

PFQuery *amountQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[amountQuery whereKey:@"remittanceAmount" equalTo:amount];

PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:beneCodeQuery,amountQuery,nil]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (PFObject *object in objects) {
            NSLog(@"%@", object);

            [self resignAllResponder];
            _paymentDetailsImage.frame = CGRectMake(_paymentDetailsImage.frame.origin.x, _paymentDetailsImage.frame.origin.y, _paymentDetailsImage.frame.size.width, paymentModeOrigHeight);
            _detailsView.alpha = 1;

        }
    }}}];
4

2 回答 2

6

每次添加约束时,都会隐式使用 AND:

[query whereKey:@"username" equalTo:@"admin"];
[query whereKey:@"location" equalTo:@"USA"];

转换为“用户名 == 'admin' AND location == 'USA'”

更新

以您的具体情况为例:

PFQuery *beneCodeQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[beneCodeQuery whereKey:@"beneCode" containsString:_beneCodeText.text];
[beneCodeQuery whereKey:@"remittanceAmount" equalTo:amount];

NSArray *result = [beneCodeQuery findObjects];
于 2014-03-13T08:38:10.327 回答
0

我最近在 Parse.com 论坛上看到了 Héctor Ramos 的这个答案:

您确实可以对单个查询有多个约束,这将被视为 AND。不支持对同一个键多次使用相同的约束。在这种情况下,您对“成员”有两个 whereKey:equalTo: 约束,因此只考虑最后一个。

相反,您需要使用 whereKey:containsAllObjectsInArray:。

PFQuery *query = [PFQuery queryWithClassName:@"Conversation"];
[query whereKey:@"members" containsAllObjectsInArray:@[aFriend.data, [PFUser currentUser]]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {...}];

我希望它有所帮助。

于 2014-03-13T08:37:53.480 回答