Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 通过实体中的多个属性查找_Sql_Magicalrecord - Fatal编程技术网

Sql 通过实体中的多个属性查找

Sql 通过实体中的多个属性查找,sql,magicalrecord,Sql,Magicalrecord,我要执行精确的sql查询: select * from table where field1 = 'x' or field2 = 'x' or field3 = 'x' 到目前为止,我只能获取实体中的一个字段: NSArray *products = [Product MR_findByAttribute:@"id" withValue:categoryID]; 假设我想获取categoryID的id和name字段,我如何在MagicalRecord中实现这一点 我可以这样做: NSArra

我要执行精确的sql查询:

select * from table where field1 = 'x' or field2 = 'x' or field3 = 'x'
到目前为止,我只能获取实体中的一个字段:

NSArray *products = [Product MR_findByAttribute:@"id" withValue:categoryID];
假设我想获取categoryID的
id
name
字段,我如何在MagicalRecord中实现这一点

我可以这样做:

NSArray *productsByID = [Product MR_findByAttribute:@"id" withValue:categoryID];
NSArray *productsByName = [Product MR_findByAttribute:@"name" withValue:categoryID];

难道没有一种单线解决方案吗?因为在处理大量字段时,这将变得有点复杂。

使用NSPredicate。比如说

[Contact MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"%K contains[cd] %@ or %K contains[cd] %@", @"name", @"jo", @"surname", @"jo"];
其中Contact是实体,%K是键的占位符(实体的属性),%@占位符是搜索值

此谓词用于查找姓名或姓氏包含jo的任何联系人

为您提供解决方案

select * from table where field1 = 'x' or field2 = 'x' or field3 = 'x'

[Product MR_findAllWithPredicate:[NSPredicate predicateWithFormat: @"%K == %@ OR %K == %@ OR %K == %@", @"field1", @"x", @"field2", @"x", @"field3", @"x"];