如何运行类似下面这样的sql query CoreData:
SELECT COUNT(*) FROM headphones WHERE id IN (SELECT id FROM old_headphones);我读了NSPredicate指南和NSExpression,但不明白怎么做。
发布于 2012-03-02 21:02:23
试试这个:
SELECT COUNT(distinct headphones.Id)
FROM headphones
INNER JOIN old_headphones ON
headphones.Id = old_headphones.Id当耳机和old_headphones之间没有n对1的关系时,可以省略DISTINCT。
发布于 2012-03-02 21:55:51
/*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 https://stackoverflow.com/questions/9533561
复制相似问题