4

如何在 ORMLite 中构建查询,以便我可以使用 orderBy 函数(使用带有原始字符串的函数或参数化的函数)引用与我正在构建查询的 dao 不同的实体的属性? 我的查询是这样构建的:

// Inner query for performances
QueryBuilder<Performance, String> performancesQB = performanceDao.queryBuilder();
performancesQB.selectColumns("performance_id");
SelectArg performanceSelectArg = new SelectArg();
performancesQB.where().lt("date", performanceSelectArg);

// Outer query for Order objects, where the id matches in the performance_id
// from the inner query
QueryBuilder<Order, String> ordersQB = orderDao.queryBuilder();
ordersQB.where().isNull("user_id").and().in("performance_id", performancesQB);
ordersQB.orderByRaw("performances.date DESC");
pastOrdersQuery = ordersQB.prepare();

每当我尝试执行此查询时遇到的异常是:

android.database.sqlite.SQLiteException: no such column: performances.date:,
   while compiling: SELECT * FROM `orders` WHERE
     (`user_id` IS NULL AND `performance_id` IN
     (SELECT `performance_id` FROM `performances` WHERE `date` < ? ) )
     ORDER BY performances.date DESC 

我在这里看到的唯一解决方案是自己使用 JOIN 而不是嵌套选择编写原始查询。这可能是一个很好的解决方案吗?

4

1 回答 1

6

ORMLite 现在支持简单的 JOIN 查询。这里是关于这个主题的文档:

http://ormlite.com/docs/join-queries

所以你的查询现在看起来像:

QueryBuilder<Performance, String> performancesQB = performanceDao.queryBuilder();
SelectArg performanceSelectArg = new SelectArg();
performancesQB.where().lt("date", performanceSelectArg);
performancesQB.orderBy("date", false);

// query for Order objects, where the id matches
QueryBuilder<Order, String> ordersQB = orderDao.queryBuilder();
ordersQB.join(performancesQB).where().isNull("user_id");
pastOrdersQuery = ordersQB.prepare();
于 2012-09-27T21:24:46.927 回答