在Ormlite中使用GROUP BY,HAVING和ORDER BY会返回错误的结果

时间:2012-11-14 17:13:44

标签: ormlite android-sqlite

以下代码给出错误

List<List<Consumption>> children = new ArrayList<List<Consumption>>();
try {
    for(DrinkType drinkType : groups){
        QueryBuilder<Consumption, ?> qb = getHelper()
                .getConsumptionDao().queryBuilder();
        qb.groupBy(Consumption.COLUMN_DRINK_ID).having("MIN("+Consumption.COLUMN_PRICE+")").orderBy(Consumption.COLUMN_PRICE, true);
        QueryBuilder<Drink, Integer> qb2 = getHelper().getDrinkDao().queryBuilder();
        qb2.where().eq(Drink.COLUMN_DRINKTYPE, drinkType.getFlag());
        qb.join(qb2);
        Log.d("Query", qb.prepareStatementString());
        children.add(qb.query());   
    }

} catch (SQLException e) {
    e.printStackTrace();
}

似乎QueryBuilder将HAVING子句置于ORDER BY子句后面而不是前面。

java.sql.SQLException: Problems executing Android query: 
SELECT `consumption`.* FROM `consumption` INNER JOIN `drink` ON
 `consumption`.`drink_id` = `drink`.`_id` WHERE `drink`.`drink_type` = 'P'
 GROUP BY `consumption`.`drink_id` ORDER BY `consumption`.`price` HAVING MIN(price) 

我尝试通过执行原始查询来解决此问题,但是如果我使用以下查询

SELECT co.* FROM consumption co INNER JOIN drink dr ON dr._id = co.drink_id
 WHERE dr.drink_type = 'P' GROUP BY co.drink_id HAVING MIN(price) ORDER BY price

与使用SQLite Manager直接在我的sqlite数据库上执行相同的查询相比,这会产生不同的结果。我认为在同一查询中使用HAVING和ORDER BY时,ORMlite可能会出现问题。

是否有可能的解决方法以便我可以使此查询起作用?

0 个答案:

没有答案