GROUP BY查询使用sqlite.query()函数

时间:2015-09-16 02:39:20

标签: android sqlite

我有sqlite工作和测试查询。

我只想将其转换为sqlitedatabase.query();函数

select distinct id, date ,sum(goal) from goal where id=0 and date = 'Sep 15, 2015' group by id, date;

如何实现这一目标?

2 个答案:

答案 0 :(得分:4)

尝试以下操作。

String table = "goal";
String[] columns = new String[] { "distinct id", "date", "sum(goal)" };
String selection = "id=? and date=?";
String[] arguments = new String[] { "0", "Sep 15, 2015" };
String groupBy = "id, date";
String having = null;
String orderBy = null;
db.query(table, columns, selection, arguments, groupBy, having, orderBy);

请参阅doc

答案 1 :(得分:0)

这是另一种方法。在查询中,右边的第三个“null”是

// Get Alla Favorite
public List<Favorite> getAllFavorite() {
    List<Favorite> foodList = new ArrayList<>();
    Favorite food;
    SQLiteDatabase database = dbHelper.getReadableDatabase();

    final String kolonlar[] = {DBHelper.COLUMN_ID_FAV, DBHelper.COLUM_NAME_FAV, DBHelper.COLUM_NAME_TARIFE,
            DBHelper.COLUMN_CALORI_FAV, DBHelper.COLUMN_PROTEIN_FAV, DBHelper.COLUMN_CARBONHYDRAT_FAV,
            DBHelper.COLUMN_FAT_FAV, DBHelper.COLUMN_ENERGY_FAV, DBHelper.COLUMN_TYPE_FAV};
    Cursor cursor = database.query(DBHelper.TABLE_NAME_FAV, kolonlar, null, null, DBHelper.COLUM_NAME_FAV, null, null);

    while (cursor.moveToNext()) {
        food = new Favorite();
        food.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID_FAV)));
        food.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUM_NAME_FAV)));
        food.setNameTarife(cursor.getString(cursor.getColumnIndex(DBHelper.COLUM_NAME_TARIFE)));
        food.setCalVal(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_CALORI_FAV)));
        food.setProtVal(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_PROTEIN_FAV)));
        food.setcHdrt(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_CARBONHYDRAT_FAV)));
        food.setFat(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_FAT_FAV)));
        food.setEnergy(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ENERGY_FAV)));
        food.setType(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TYPE_FAV)));
        foodList.add(food);
    }
    database.close();
    cursor.close();
    return foodList;
}