首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >核心数据函数类似于WHERE='(sql query)‘

核心数据函数类似于WHERE='(sql query)‘
EN

Stack Overflow用户
提问于 2012-03-02 20:48:00
回答 2查看 265关注 0票数 0

如何运行类似下面这样的sql query CoreData:

代码语言:javascript
复制
SELECT COUNT(*) FROM headphones WHERE id IN (SELECT id FROM old_headphones);

我读了NSPredicate指南和NSExpression,但不明白怎么做。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-02 21:02:23

试试这个:

代码语言:javascript
复制
SELECT COUNT(distinct headphones.Id) 
FROM headphones 
    INNER JOIN old_headphones ON
        headphones.Id = old_headphones.Id

当耳机和old_headphones之间没有n对1的关系时,可以省略DISTINCT。

票数 0
EN

Stack Overflow用户

发布于 2012-03-02 21:55:51

代码语言:javascript
复制
/*First request*/
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"headphones" inManagedObjectContext:yourContext];
[fetchRequest setEntity:entity];

NSError *error;
NSArray *fetchedObjects = [yourContext executeFetchRequest:fetchRequest error:&error];

/*2nd request*/
NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity2 = [NSEntityDescription 
                               entityForName:@"old_headphones" inManagedObjectContext:yourContext];
[fetchRequest setEntity:entity2];

NSError *error;
NSArray *fetchedObjects = [yourContext executeFetchRequest:fetchRequest2 error:&error];

//use your 2 NSArray to find your headphones 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9533561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档