抱歉,我对SQL语法了解不多。基本上,我的表中有以下列:
ID GAME DRAW NUMBER
x 0 23
x 1 34
x 0 24
x 1 35
x 1 33
我希望根据GAME列值获得最大抽奖号码。 例如,如果我选择游戏1,我想获得35的抽奖号码(即游戏1的最高抽奖号码为35)
我想出的最好成绩如下:
String table = "xxx", col = "DRAW NUMBER", game_col = "GAME";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(table, null, col+"=(SELECT MAX("+col+") FROM " + table + " GROUP BY " + game_col + ")", null, null, null, null);
此外,我想根据GAME值获取特定游戏的数量。 这就是我到目前为止所做的:
int game_val = 1, row_count;
String table = "xxx", game_col = "GAME";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT COUNT(*) FROM " + table + " WHERE " + game_col + " = " + game_val + "", null);
row_count = c.getCount();
提前谢谢!
答案 0 :(得分:3)
您可以使用rawQuery
对于特定游戏,只需将1替换为变量(已编辑:已移除组,此处不需要,感谢Bae)
Cursor c = db.rawQuery("SELECT max(DRAW NUMBER) FROM xxx WHERE GAME = 1", null);
所有游戏的所有最大数量列表
Cursor c = db.rawQuery("SELECT GAME, max(DRAW NUMBER) FROM xxx GROUP BY GAME", null);
答案 1 :(得分:1)
您只需要此查询
Cursor cursor = db.rawQuery("SELECT MAX(DRAW_NUMBER) FROM your_table_here WHERE GAME = 1, null);
对于列表,您可以使用Hrusilov告诉您的查询。