可视化来自Android中的JOIN查询SqLite的数据

时间:2018-02-06 20:09:39

标签: join android-sqlite android-cursoradapter

我第一次在为Android编程时偶然发现了使用SqlLite进行INNER JOIN的概念。 我设法得到了回报但现在的问题是我想在List中显示数据而我无法按照我的意图显示信息。

以下是检索数据的查询

cursor = database.rawQuery("SELECT matchesbet._id, hometeam, awayteam, gamble " +
                    "FROM matchesbet INNER JOIN gambles ON matchesbet.idmatch = gambles.idmatch " +
                    "WHERE matchesbet.idbet = ? GROUP BY matchesbet.idmatch ORDER BY matchesbet._id DESC", selectionArgs);

数据用于在bindView方法

中扩展CursorAdapter的类中
    TextView tvGamble = (TextView) view.findViewById(R.id.gamble_text_view);

    // Extract properties from cursor
    int gamble = cursor.getInt(cursor.getColumnIndex("gamble"));

    tvGamble.setText(String.valueOf(gamble));

现在在DB中我为每场比赛保存了多个“赌博”,但我唯一可以显示的是1次赌博。

Db表结构和样本数据

MATCHESBET
    +-----+-------+---------+----------+-----------+--+
    | id  | idbet | idmatch | hometeam | awayteam  |  |
    +-----+-------+---------+----------+-----------+--+
    | 349 |  3000 |      27 | TeamA    | TeamZ     |  |
    | 350 |  2456 |      39 | TeamC    | TeamG     |  |
    | 351 |  2818 |      20 | TeamW    | TeamB     |  |
    +-----+-------+---------+----------+-----------+--+

    GAMBLES
    +-----+---------+--------+-------+
    | _id | idmatch | gamble | idbet |
    +-----+---------+--------+-------+
    | 349 |      27 | WIN    |  3000 |
    | 350 |      27 | LOST   |  3000 |
    | 351 |      27 | DRAW   |  3000 |
    | 489 |    1345 | WIN    |  1981 |
    +-----+---------+--------+-------+

考虑到我使用ID = 3000过滤Bet,我希望有一个listItem与TeamA vs TeamZ(ID = 27),并在同一行中输入字符串WIN,LOST,DRAW。然后再次迭代所有IDBET = 3000

的匹配

我不确定如何摆脱这种情况 希望有人能提供帮助 干杯

1 个答案:

答案 0 :(得分:0)

我相信你想要的就像: -

SELECT matchesbet._id, hometeam, awayteam, group_concat(gamble) AS gamble 
FROM matchesbet JOIN gambles ON  gambles.idmatch = matchesbet.idmatch
WHERE matchesbet.idbet = 3000 
GROUP BY matchesbet.idmatch 
ORDER BY matchesbet._id DESC;

即。保持GROUP,而不是选择列gamble,选择 group_concat(gamble) AS gamble 列。

默认情况下,值以 , 分隔。但是,您可以通过第二个参数指定另一个分隔符group_concat(gamble,' - ')会 给: -

enter image description here

您可能会发现此链接有用Aggregate Functions