基本上我想做类似
的事情SELECT * FROM FriendRequests WHERE (from == user OR to == user) AND status = "accepted"
有一种名为orQueryWithSubqueries
的方法可以让我将这两种方法结合起来,但我无法弄清楚如何在这些方法和状态查询之间执行AND。
let statusCheck = FriendRequest.query()
statusCheck.whereKey("status", equalTo: FriendRequest.Status.Accepted.rawValue)
let userCheckOne = FriendRequest.query()
userCheckOne.whereKey("from", equalTo: User.currentUser())
let userCheckTwo = FriendRequest.query()
userCheckOne.whereKey("to", equalTo: User.currentUser())
let userCheck = PFQuery.orQueryWithSubqueries([userCheckOne, userCheckTwo])
// Some how do (userCheck AND statusCheck)
答案 0 :(得分:3)
如果您从SQL角度来看它,请将OR
查询更像UNION
语句。
您的查询将如下所示:
SELECT *
FROM FriendRequests
WHERE from = user
AND status = "accepted"
UNION
SELECT *
FROM FriendRequests
WHERE to = user
AND status = "accepted"
正如您所看到的,解决方案是简单地将AND
部分添加到您的两个查询中:
let userCheckOne = FriendRequest.query()
userCheckOne.whereKey("from", equalTo: User.currentUser())
userCheckOne.whereKey("status", equalTo: FriendRequest.Status.Accepted.rawValue)
let userCheckTwo = FriendRequest.query()
userCheckTwo.whereKey("to", equalTo: User.currentUser())
userCheckTwo.whereKey("status", equalTo: FriendRequest.Status.Accepted.rawValue)
let userCheck = PFQuery.orQueryWithSubqueries([userCheckOne, userCheckTwo])