1

我想通過不同的實體進行NSPredicate。這是我的核心數據庫。NSPredicate over different entities

enter image description here

爲簡單起見,這是查詢我應該做的。

Select c.c_name From Company c, Person p Where c.c_id = p.cu_company_id 

現在我想知道我的謂詞應該是什麼樣子,如果我想實現上述查詢的結果。

回答

1

假設C_ID和cu_company_id是整數,你可以嘗試

​​

,一旦你擁有者數組,然後你可以遍歷,並得到對方的姓名。爲了得到它

NSFetchRequest *fr = [[NSFetchRequest alloc] initWithEntityName:@"Company"]; 
    NSError *error; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"c_id == data.person.cu_company_id"]; 
    [fr setPredicate:predicate]; 
    NSArray *companies = [self.managedObjectContext executeFetchRequest:fr error:&error]; 

一旦你有公司的數組,你可以循環通過它,並獲得公司的名稱。

+0

對不起,我需要公司的名字而不是人名。 – Steaphann

+0

這是相同的事情,反過來。只需考慮對象而不是表格,行和列就很容易。我編輯了答案。 – Srikanth

+0

Oké但是如果我需要一個人對象,並且不希望從這個人反對公司名稱,但公司名稱。我應該提出兩個單獨的提取請求嗎? – Steaphann