核心数据中的 SQL 请求

发布于 2024-11-26 22:21:56 字数 879 浏览 1 评论 0原文

我知道如何在 SQL 中编写我需要的请求,但找不到使用 Core Data 实现它的方法,或者如果我找到解决方案,它会很丑陋且混乱,使用 20 多行代码...

我的请求是SELECT MAX(distance) FROM DataTrip WHERE idTrip = (SELECT DISTINCT(idTrip) FROM DataTrip);

知道如何使用带有 NSSortDescriptor 的核心数据来做到这一点, NSPredicate、NSExpressionDescription 等?

谢谢你!

更新 1

DataTrip 对象的数据模型:

@interface DataTrip : NSManagedObject {
@private
}
@property (nonatomic, retain) NSNumber * speed;
@property (nonatomic, retain) NSNumber * heading;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSNumber * timestamp;
@property (nonatomic, retain) NSNumber * latitude;
// More properties here
@property (nonatomic, retain) NSNumber * idTrip;
@property (nonatomic, retain) NSNumber * distance;

@end

因此,我的数据库中会有多个具有相同 idTrip 的行,并且我想要每个 idTrip 的距离最大值。

I know how to write the request I need in SQL but can't find a way to implement it using Core Data, or if I find a solution it would be ugly and messy, using 20+ lines of code...

My request is SELECT MAX(distance) FROM DataTrip WHERE idTrip = (SELECT DISTINCT(idTrip) FROM DataTrip);

Any idea how I can do that using Core Data with NSSortDescriptor, NSPredicate, NSExpressionDescription, etc.?

Thank you!

Update 1

Data model for DataTrip object:

@interface DataTrip : NSManagedObject {
@private
}
@property (nonatomic, retain) NSNumber * speed;
@property (nonatomic, retain) NSNumber * heading;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSNumber * timestamp;
@property (nonatomic, retain) NSNumber * latitude;
// More properties here
@property (nonatomic, retain) NSNumber * idTrip;
@property (nonatomic, retain) NSNumber * distance;

@end

So I would have in my database multiple rows with the same idTrip, and I want the max value for distance for each idTrip.

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

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

发布评论

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

评论(1

屋顶上的小猫咪 2024-12-03 22:21:56

这个问题很难回答,因为它取决于 SQL 和 Core Data 相互关联的假设。事实上,他们彼此之间没有什么关系。 Core Data 不是数据库,不应被视为或视为数据库。您完成问题中指定的任务的方式很大程度上取决于您创建的数据模型。如果没有这些信息,就不可能提供更多有用的信息。

The question is difficult to answer because it hinges on the assumption that SQL and Core Data have anything to do with each other. They in fact, have very little to do with each other. Core Data is not a database and should not be thought of as or treated as one. The way you would accomplish the task you specified in the question is highly dependent on the data model that you have created. Without that information it's not possible to give any more helpful information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文