SQL

相当于核心数据的GROUP BY

发布于 2021-03-11 17:47:19

我知道我可以使用@distinctUnionOfObjects在SQL中找到类似以下内容的东西:

SELECT a_value
FROM my_table
GROUP BY a_value;

我正在寻找的是数组中返回的 所有数据 ,而不仅仅是与按表达式匹配的值的数组。本质上,我在寻找与以下SQL查询等效的核心数据:

SELECT *
FROM my_table
GROUP BY a_value;
关注者
0
被浏览
113
1 个回答
  • 面试哥
    面试哥 2021-03-11
    为面试而生,有面试问题,就找面试哥。

    这是模拟的

    SELECT 'Status', COUNT(*) FROM 'Records' GROUP BY 'Status'

    NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Record"];
    NSEntityDescription* entity = [NSEntityDescription entityForName:@"Record"
                                              inManagedObjectContext:myManagedObjectContext];
    NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"status"];
    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"url"]; // Does not really matter
    NSExpression *countExpression = [NSExpression expressionForFunction: @"count:"
                                                              arguments: [NSArray arrayWithObject:keyPathExpression]];
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName: @"count"];
    [expressionDescription setExpression: countExpression];
    [expressionDescription setExpressionResultType: NSInteger32AttributeType];
    [fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, expressionDescription, nil]];
    [fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
    [fetch setResultType:NSDictionaryResultType];
    NSError* error = nil;
    NSArray *results = [myManagedObjectContext executeFetchRequest:fetch
                                                             error:&error];
    

    在这里找到



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看