在Realm中使用Join查询

时间:2016-07-08 17:35:27

标签: objective-c realm

我正在为我的项目使用FMDatabase更改为Realm。但是,我的SQL查询有这样的问题:

SELECT a.areaId, a.areaName, orderId, t.accountId, t.accountNo,
       amount, customerId, customerName, customerCategoryId,
       customerCategoryName, i.dispStatus, t.locationId,
       locationName, totalAmount, orderCode, orderFullName,
       t.tableId, t.tableName, tableTypeId, productAmount
FROM TableTable t
LEFT JOIN AreaTable a ON t.areaId = a.areaId
LEFT OUTER JOIN OrderTable i ON t.tableId = i.tableId
                            AND (i.dispStatus='4' OR i.dispStatus='2' OR i.dispStatus='5') 
WHERE t.accountId = %@ AND t.locationId = %@

我不知道如何将其转换为Realm语法。

AreaModel:

@property (strong, nonatomic) NSString *areaId;
@property (strong, nonatomic) NSString *areaName;
@property (assign, nonatomic) NSString *position;
@property (strong, nonatomic) NSString *dispStatus;
@property (strong, nonatomic) NSString *isDefault;
@property (strong, nonatomic) NSString *locationId;
@property (strong, nonatomic) NSString *accountId;
@property (strong, nonatomic) NSString *accountNo;
@property (strong, nonatomic) NSString *regDttm;
@property (strong, nonatomic) NSString *regUser;
@property (strong, nonatomic) NSString *langId;

的TableModel:

@property (strong, nonatomic) NSString *tableId;
@property (strong, nonatomic) NSString *areaId;
@property (strong, nonatomic) NSString *tableTypeId;
@property (strong, nonatomic) NSString *tableName;
@property (strong, nonatomic) NSString *available;
@property (strong, nonatomic) NSString *topPosition;
@property (strong, nonatomic) NSString *leftPosition;
@property (strong, nonatomic) NSString *dispStatus;
@property (strong, nonatomic) NSString *isDefault;
@property (strong, nonatomic) NSString *locationId;
@property (strong, nonatomic) NSString *accountId;
@property (strong, nonatomic) NSString *accountNo;
@property (strong, nonatomic) NSString *regDttm;
@property (strong, nonatomic) NSString *regUser;
@property (strong, nonatomic) NSString *updDttm;
@property (strong, nonatomic) NSString *updUser;

OrderModel:

@property (strong, nonatomic) NSString *orderId;
@property (strong, nonatomic) NSString *orderCode;
@property (strong, nonatomic) NSString *orderType;
@property (strong, nonatomic) NSString *orderName;
@property (strong, nonatomic) NSString *orderDate;
@property (strong, nonatomic) NSString *orderUser;
@property (strong, nonatomic) NSString *orderFullName;
@property (strong, nonatomic) NSString *customerCode;
@property (strong, nonatomic) NSString *customerPhone;
@property (strong, nonatomic) NSString *orderDescription;
@property (strong, nonatomic) NSString *paymentType;
@property (strong, nonatomic) NSString *updDttm;
@property (strong, nonatomic) NSString *updUser;
@property (strong, nonatomic) NSDecimalNumber *areaId;
@property (strong, nonatomic) NSDecimalNumber *tableId;
@property (strong, nonatomic) NSString *isDirty;
@property (strong, nonatomic) NSString *locationPhone;
@property (strong, nonatomic) NSString *reduction;
@property (strong, nonatomic) NSDecimalNumber *accountId;
@property (strong, nonatomic) NSString *accountNo;
@property (strong, nonatomic) NSString *regUser;
@property (strong, nonatomic) NSString *regDttm;
@property (strong, nonatomic) NSDecimalNumber *locationId;
@property (strong, nonatomic) NSString *locationName;
@property (strong, nonatomic) NSDecimalNumber *customerId;
@property (strong, nonatomic) NSString *customerName;
@property (strong, nonatomic) NSDecimalNumber *customerCategoryId;
@property (strong, nonatomic) NSString *customerCategoryName;
@property (strong, nonatomic) NSString *dispStatus;
@property (strong, nonatomic) NSString *invoiceDate;
@property (strong, nonatomic) NSDecimalNumber *quantity;
@property (strong, nonatomic) NSDecimalNumber *productAmount;
@property (strong, nonatomic) NSDecimalNumber *wholesaleAmount;
@property (strong, nonatomic) NSDecimalNumber *productReductionAmount;
@property (strong, nonatomic) NSDecimalNumber *totalAmount;
@property (strong, nonatomic) NSDecimalNumber *reductionAmount;
@property (strong, nonatomic) NSDecimalNumber *reductionPercent;
@property (strong, nonatomic) NSDecimalNumber *amount;
@property (strong, nonatomic) NSDecimalNumber *cashAmount;
@property (strong, nonatomic) NSDecimalNumber *changeAmount;
@property (strong, nonatomic) NSDecimalNumber *profitAmount;
@property (strong, nonatomic) NSDecimalNumber *loyaltyPoints;
@property (strong, nonatomic) NSString *channelType;
@property (strong, nonatomic) NSString *deviceType;
@property (strong, nonatomic) NSDecimalNumber *storeId;
@property (strong, nonatomic) NSString *storeName;
@property (strong, nonatomic) NSString *invoiceFullName;
@property (strong, nonatomic) NSString *invoiceCode;
@property (strong, nonatomic) NSString *invoiceUser;
@property (strong, nonatomic) NSString *reductionDisplay;
@property (strong, nonatomic) NSString *areaName;
@property (strong, nonatomic) NSString *tableName;
@property (strong, nonatomic) NSString *accessType;
@property (strong, nonatomic) NSString *invoiceType;
@property (strong, nonatomic) NSDecimalNumber *payAmount;
@property (strong, nonatomic) NSDecimalNumber *paymentByCash;
@property (strong, nonatomic) NSDecimalNumber *paymentByVoucher;
@property (strong, nonatomic) NSDecimalNumber *loanAmount;
@property (strong, nonatomic) NSString *paymentStatus;
@property (strong, nonatomic) NSString *customerIsDefault;
@property (strong, nonatomic) NSDecimalNumber *taxAmount;
@property (strong, nonatomic) NSDecimalNumber *preTaxAmount;
@property (strong, nonatomic) NSString *taxDescription;
@property (strong, nonatomic) NSString *refTransactionCode;
@property (strong, nonatomic) NSString *username;
@property (strong, nonatomic) NSString *userFullName;
@property (strong, nonatomic) NSString* receiveIssue;

1 个答案:

答案 0 :(得分:2)

Since Realm is an object database rather than a relational database, relationships between objects are expressed directly rather than indirectly using primary and foreign keys. For instance, your Table class would have express that it has an Area using:

@property (nonatomic) Area *area;

Relationships such as this can be used in queries:

[Table objectsWhere:@"area.name = 'Some Area'"]

Note that using relationships like this doesn't preclude giving instances unique identifiers. Unique identifiers can still be useful for mapping local objects to objects on a web service, or to help with handing objects between threads in your local application.