GROUP BY 相当于 Core Data

编程入门 行业动态 更新时间:2024-10-20 13:51:05
本文介绍了GROUP BY 相当于 Core Data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I know that I can use @distinctUnionOfObjects to find something like the following in SQL:

SELECT a_value FROM my_table GROUP BY a_value;

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

What I'm looking for is all of the data returned in an array, not just the array of values that matches the group by expression. Essentially, I'm looking for the equivalent of the following SQL query for core data:

SELECT * FROM my_table GROUP BY a_value;

推荐答案

It's analog

It's analog

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];

在这里找到

更多推荐

GROUP BY 相当于 Core Data

本文发布于:2023-11-15 03:50:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:GROUP   Core   Data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!