Ormlite内连接三个表

时间:2011-09-04 18:49:20

标签: android sql inner-join ormlite

我想在三个表上创建一个内连接,例如:

SELECT C.Description, D.ItemDescription
  FROM OrderDetailStatement AS D 
 INNER JOIN OrderHeaderStatement AS H 
    ON H.OrderHeaderStatementRefID = D.OrderHeaderStatementRefID 
 INNER JOIN customers AS C 
    ON H.CustomerRefID = C.CustomerRefID
 WHERE (D.MixedValue > 1000)

但是我有点困惑,你能不能给我一个演练?

提前致谢

1 个答案:

答案 0 :(得分:36)

ORMLite现在支持simple JOIN statements。您可以执行以下操作:

// start the order header query
QueryBuilder<OrderHeader, Integer> orderHeaderQb = orderHeaderDao.queryBuilder();
QueryBuilder<Customer, Integer> customerQb = customerDao.queryBuilder();
// join with the order query
orderHeaderQb.join(customerQb);
// start the order statement query
QueryBuilder<OrderStatement, Integer> orderStatementQb =
    orderStatementDao.queryBuilder();
orderStatementQb.where().gt("mixedvalue", 100);
// join with the order-header query
orderStatementQb.join(orderHeaderQb);
List<OrderStatement> orderStatementQb.query();

但请注意,您只能使用此机制从查询构建器中获取实体。如果您想从不同的对象获取两个描述字段,那么您仍然必须使用原始查询。

支持“原始查询”,包括Dao.queryRaw()方法,您可以使用自己的SQL。我怀疑你已经找到了它们。以下是docs for raw queries