gpt4 book ai didi

swift - Parse - 使用 OR 和 AND 的复合查询?

转载 作者:行者123 更新时间:2023-11-28 11:21:16 24 4
gpt4 key购买 nike

基本上我想做类似的事情

SELECT * FROM FriendRequests WHERE (from == user OR to == user) AND status = "accepted"

有一个名为 orQueryWithSubqueries 的方法,它允许我将两个 or 结合起来,但我不知道如何在它们和状态查询之间执行 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)

最佳答案

如果您从 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])

关于swift - Parse - 使用 OR 和 AND 的复合查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com