在IOS CoreData中,多表查詢上相對來說,沒有SQL直觀,但COREDATA的功能還是可以完成相關操作的。
多表查詢,表與表之間肯定存在某種關係,如果對於類似外串連,左串連等操作,在COREDATA中就顯得無力(請高手指教了)。
在上節中,介紹了一下資料庫的關係查詢操作。
下面使用CoreData進行關聯式資料庫的表與表之間的關係示範。
產生COREDATA和如何設定關係就不再詳談了,見之前的文章。
建立好的關係圖:
一步步建立上面關係圖:
先建立部門表,員工表,職位表,工資等級表,開戶銀行表
上面建立表之後,我們還需要建立表之間的關係
部門和員工之間的關係:1 V N
部門和職位的關係:1 V N
員工與職位的關係:多對一關聯性
員工與開戶行:一個員工只能提供一個開戶行,但一個開戶行可以給多名員工進行開卡。所以關係為N V 1;
職位和工資等級:一個職位只對應一個工資等級;1V1
下面插入測試資料:
- (IBAction)onbtnclick:(id)sender
{
Department *dept = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
dept.dp_deptname = @"HR";
Department *dept2 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
dept2.dp_deptname = @"DEV";
Department *dept3 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
dept3.dp_deptname = @"POD";
Salary *sy =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
sy.sy_level = @"D";
sy.sy_scale = [NSNumber numberWithDouble:0.1];
Salary *sy2 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
sy2.sy_level = @"C";
sy2.sy_scale = [NSNumber numberWithDouble:0.15];
Salary *sy3 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
sy3.sy_level = @"B";
sy3.sy_scale = [NSNumber numberWithDouble:0.4];
Salary *sy4 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
sy4.sy_level = @"A";
sy4.sy_scale = [NSNumber numberWithDouble:0.8];
Post *pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt.pt_name = @"行政專員";
pt.dept = dept;
Post *pt2= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt2.salary = sy3;
pt2.pt_name = @"人事經理";
pt2.dept = dept;
Post *pt3= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt3.pt_name = @"開發工程師";
pt3.dept = dept2;
pt3.salary = sy2;
pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt.pt_name = @"架構師";
pt.dept = dept2;
pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt.pt_name = @"專案經理";
pt.dept =dept2;
Post *pt6= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt6.pt_name = @"測試工程師";
pt6.dept = dept2;
pt6.salary = sy;
pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt.pt_name = @"銷售代表";
pt.dept = dept3;
pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt.pt_name = @"銷售經理";
pt.dept = dept3;
Post *pt9= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt9.pt_name = @"大客戶經理";
pt9.dept = dept3;
pt9.salary = sy4;
Bank *bk = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
bk.bk_name = @"招行";
bk.bk_address = @"廣州";
Bank *bk2 = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
bk2.bk_name = @"浦發";
bk2.bk_address = @"上海";
Bank *bk3 = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
bk3.bk_name = @"工行";
bk3.bk_address = @"深圳";
Employee *em = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em.em_age = [NSNumber numberWithInt:20];
em.em_name = @"張三";
em.em_sex = [NSNumber numberWithInt:1];
em.em_bankcardid = @"46326587439043";
em.dept = dept2;
em.post = pt3;
em.bank = bk2;
em = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em.em_age = [NSNumber numberWithInt:18];
em.em_name = @"李四";
em.em_sex = [NSNumber numberWithInt:2];
em.em_bankcardid = @"32565443246567";
em.dept = dept;
em.post = pt2;
em.bank = bk3;
em = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em.em_age = [NSNumber numberWithInt:26];
em.em_name = @"歐陽";
em.em_sex = [NSNumber numberWithInt:2];
em.em_bankcardid = @"14354654656767";
em.dept = dept3;
em.post = pt9;
em.bank = bk3;
em = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em.em_age = [NSNumber numberWithInt:22];
em.em_name = @"歐陽";
em.em_sex = [NSNumber numberWithInt:2];
em.em_bankcardid = @"9873429837433";
em.dept = dept2;
em.post = pt6;
em.bank = bk3;
[self.managedObjectContext save:nil];
}
資料庫中資料:
1、查詢開發部門中名為張三的工資等級
NSEntityDescription * emEty = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *frq = [[[NSFetchRequest alloc]init]autorelease];
[frq setEntity:emEty];
NSPredicate * cdt = [NSPredicate predicateWithFormat:@"em_name = %@",@"張三"];
[frq setPredicate:cdt];
NSArray *objs =[self.managedObjectContext executeFetchRequest:frq error:nil];
//NSLog(@"%i",[objs count]);
NSLog(@"%@",((Employee*)[objs objectAtIndex:0]).post.salary.sy_level);
2、查詢營運部名為歐陽的工資等級及開戶銀行
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetch = [[[NSFetchRequest alloc]init]autorelease];
[fetch setEntity:entity];
NSPredicate * qcmd = [NSPredicate predicateWithFormat:@"em_name = %@ ",@"歐陽"];
[fetch setPredicate:qcmd];
NSArray * obs = [self.managedObjectContext executeFetchRequest:fetch error:nil];
NSPredicate * filter = [NSPredicate predicateWithFormat:@"dept.dp_deptname = %@",@"POD"];
NSArray * ret = [obs filteredArrayUsingPredicate:filter]; //從數組中進行過濾。
NSLog(@"%@",((Employee*)[ret objectAtIndex:0]).post.salary.sy_level);
多表查詢主要就在於表之間建立好相關的關聯關係(relationship),其次就是充分的使用NSPredicate 這個查詢條件來進行過濾。