2

如何在 Core Data(iOS 开发)中进行这个 SQLite 查询?

SELECT DISTINCT VALUE_1, VALUE_2 FROM TABLE;

它应该返回 (duo, trio) 不同的值,例如,从此表中:

(123, 234, Car)
(123, 456, Bus)
(234, 456, Train)
(123, 456, Eletric Bus)
(234, 456, Subway)

它应该返回 3 行,因为(123, 234)是 UNIQUE 等等(234, 456)(123, 456)

(123, 234, Car)
(234, 456, Train) or (234, 456, Subway)
(123, 456, Eletric Bus) or (123, 456, Bus)

使用此代码,我只能区分一个值:

NSDictionary *entityProperties = [entity propertiesByName];
[request setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"VALUE_1"]]];
[request setReturnsDistinctResults:YES];
[request setResultType:NSDictionaryResultType];
4

1 回答 1

0

您可以指定多个要获取的唯一属性:

[request setPropertiesToFetch:@[@"VALUE_1", @"VALUE_2"]];
[request setReturnsDistinctResults:YES];
[request setResultType:NSDictionaryResultType];

(请注意,您可以提供属性名称列表而不是属性描述。)

这应该返回一个字典数组,如下所示:

[
   { "VALUE_1" = 123; “VALUE_2” = 234},
   { "VALUE_1" = 234; “VALUE_2” = 456},
   { "VALUE_1" = 123; “VALUE_2” = 456},
]

但是,没有办法(据我所知)使 fetch 请求另外返回来自“随机选择”对象的第三个属性。

于 2013-06-21T11:52:20.403 回答