根据关系表从父表获取记录 - CoreData

发布于 2024-11-17 20:23:38 字数 463 浏览 10 评论 0原文

抱歉,如果标题没有任何意义。

让我进一步解释一下。

我有这个 CoreData 应用程序,而且我对 CoreData 还比较陌生。我有两张桌子。一是预算,一是支出。因此预算表有一个名为 TotalBudget 的字段。现在,我在支出表中有不同的行,用户随时间输入这些行,并且该表有一个名为“个人支出”的字段。

现在我希望做的是只获得个人支出总和小于总预算(预算表)的预算。在 SQL 语言中,它会类似于下面的内容

Select BudgetName 从预算来看 其中总预算 < (从支出中选择 Sum(IndividualBudget),其中 BudgetID = Budget.BudgetID)

如何在核心数据中实现这一目标?我有从一个表中检索数据的代码,我可以使用 NSPredicate 根据 BudgetStartDate 和 BudgetEndDate 过滤记录,但我怎样才能实现类似的目标。

谢谢

Sorry if title does not make any sense.

Let me explain it a bit further.

I have this CoreData application and I am relatively new in CoreData. I have two tables. One is Budget and one is Spending. So Budget table has a field called TotalBudget. Now I have different rows in Spending Table which user input over the time and this table has a field called Individual Spend.

Now what I am hoping to do is to get only those budgets where Sum of Individual Spend is less than TotalBudget (Budget table). In SQL language it would be something like below

Select BudgetName
From Budget
Where TotalBudget < (Select Sum(IndividualBudget) From Spend Where BudgetID = Budget.BudgetID)

How can I achieve that in core data? I have the code to retrieve data from one table and I can use NSPredicate to filter records based to BudgetStartDate and BudgetEndDate but how can I achieve something like that.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-11-24 20:23:38

假设以下对象模型:

Budget
--amount (decimal)
--spend (relationship to IndividualSpend object)

IndividualSpend
--amount (decimal)

这将为您提供一组预算实体,其中支出金额 >预算金额:

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
fetch.entity = [managedObjectModel objectForKey:@"Budget"];
fetch.predicate = [NSPredicate predicateWithFormat:@"[email protected] > amount"];

NSError *error = nil;
NSArray *results = [context executeFetchRequest:fr error:&error];
if (error) {
  NSLog(@"ERROR: %@", error);
}
NSLog(@"Results: %@", results);

Assuming following object model:

Budget
--amount (decimal)
--spend (relationship to IndividualSpend object)

IndividualSpend
--amount (decimal)

This will get you an array of Budget entities where the spend amount is > budget amount:

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
fetch.entity = [managedObjectModel objectForKey:@"Budget"];
fetch.predicate = [NSPredicate predicateWithFormat:@"[email protected] > amount"];

NSError *error = nil;
NSArray *results = [context executeFetchRequest:fr error:&error];
if (error) {
  NSLog(@"ERROR: %@", error);
}
NSLog(@"Results: %@", results);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文