通常使用 Core Data,您将设计和使用满足您需求的数据模型。您不应该从 SQL 的角度来考虑它——它甚至不必使用 SQL 进行存储——也不应该尝试将 SQL 查询直接转换为带有谓词的 fetch 请求等。
也就是说,在构建谓词时考虑 SQL WHERE 子句有时会有所帮助,但真正获取请求归结为您需要获取什么实体以及如何过滤和/或排序集合等。
获取请求仅限于单个实体,因此我认为您需要多个获取请求来模拟您的查询。
你的 Core Data 数据模型是什么样的?你想达到什么目的?你试过什么了?
更新
听起来您的数据模型包括两个实体:Drink 和 Ingredient,它们之间存在多对多关系:
饮料 <<-->> 成分
请注意,在 Core Data 中,没有 DrinkIngredient 实体,除非您明确创建它(多对多关系还有一个附加表,但它已从您那里抽象出来)。由于您想要一个与附加表中的行关联的金额值,我建议在 Core Data 中添加一个 DrinkIngredient 实体:
饮料 <-->> 饮料成分 <<--> 成分
注意:DrinkIngredient 恰好有一种饮料和一种成分。饮料可以有许多 DrinkIngredients 并且成分可以被许多 DrinkIngredients 使用。
听起来您想获取特定饮料成分列表的名称和数量。为此,只需使用如下过滤谓词获取 DrinkIngredient 对象:
// assuming "aDrink" is a reference to a particular Drink object
// and "drink" is the relationship from DrinkIngredient to Drink
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"drink == %@",aDrink];
// if the fetch request result array is named "ingredientList"
// and the relationship from DrinkIngredient to Ingredient is "ingredient"
for (DrinkIngredient *di in ingredientList) {
NSString *ingredientName = di.ingredient.name;
NSUInteger amount = di.amount.integerValue;
// use "ingredientName" and "amount" here
}
由于您使用的是 Core Data 而不是 SQL,因此您的处理方式有所不同。例如,如果您想显示所有饮品的名称和数量的成分列表,您只需获取所有 Drink 对象(无过滤谓词),然后通过从 Drink 到 DrinkIngredient 的关系访问成分。
同样,您应该考虑您正在尝试完成什么,并适当地设计和使用您的数据模型。您不应该考虑 SQL 或查询。