ios - Transfer mysql query to NSPredicate -


i'm writing mysql query, gets info correctly, cannot write nspredicate this, because don't know how select in (select) construction.

select * zphoto zphotoid in (select zalbumid zphoto) , zalbumid = 0; 

i take rows, albumid = 0, , if albumid contains photoid .

and part of nspredicate:

nsentitydescription *entity = [nsentitydescription entityforname:[nsstring stringwithformat:@"%@",name] inmanagedobjectcontext:context]; etchrequest.predicate = [nspredicate predicatewithformat:@"photoid in"] 

updated: logic: there table : screenshot here. need return row albumid = 0 , if photoid row found in albumid entity. in example need return first row. logic: if albumid = 0, album, if albumid != 0, means,that photo belongs 1-st photo album. need return albums, number of photos not nil. predicate, returns albums, albums without photos .

"albumid = 0" 

so need filter albums without photos. please me

updated 2:

return rows without photos,i think trouble (photoid in %@) ,i think found in zpk,not in albumid entity.

        nsfetchrequest *fr = [[nsfetchrequest alloc]init];         fr.predicate = [nspredicate predicatewithformat:@"albumid != %i",statsubid]; // != 0         [fr setentity:entity];          nserror *error;         nsarray *albumids = [context executefetchrequest:fr error:&error];          nssortdescriptor *sort = [[nssortdescriptor alloc]initwithkey:@"photoid" ascending:yes];         [fr setsortdescriptors:[nsarray arraywithobject:sort]];          fetchrequest.predicate = [nspredicate predicatewithformat:@"photoid in %@ , albumid == %i", albumids,statsubid]; 

updated 3

it seems working solution. thank guys!

fetchrequest.predicate = [nspredicate predicatewithformat:@"albumid != %i",statsubid]; // != 0  nserror *error; nsarray *albumids = [context executefetchrequest:fetchrequest error:&error];  nsmutablearray *albid = [nsmutablearray array]; (photo *photo in albumids) {     [albid addobject:photo.albumid]; }  fetchrequest.predicate = [nspredicate predicatewithformat:@"photoid in %@ , albumid == %i", albid,statsubid]; 

i don't think there way achieve single core data fetch request. have in 2 steps:

  • fetch objects albumid != 0 , store resulting ids in array albumids.
  • use predicate

    [nspredicate predicatewithformat:@"albumid == 0 , photoid in %@", albumids] 

    to desired result.


Comments