【问题标题】:Visualization of data coming from a JOIN query SqLite in AndroidAndroid 中来自 JOIN 查询 SqLite 的数据的可视化
【发布时间】:2018-02-06 20:09:39
【问题描述】:

在为 Android 编程时,我第一次偶然发现了 INNER JOIN 与 SqlLite 的概念。 我设法找回了一些东西,但现在的问题是我想在列表中显示数据,而我无法按预期显示信息。

这是检索数据的查询

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));

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

数据库表结构和样本数据

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,基于上述数据,我想要一个包含 TeamA 与 TeamZ(ID = 27)的 listItem,并且在同一行中包含字符串 WIN、LOST、DRAW。然后再次遍历 IDBET = 3000

的所有匹配项

我不知道如何摆脱这种情况 希望有人可以帮助 干杯

【问题讨论】:

  • 如果添加行(检索光标后)Log.d("CRSCOUNT","Number of rows in Cursor is " + cursor.getCount());,运行并且计数不大于 1,那么很可能是 WHERE 条件造成的。如果是这样,那么也许尝试使用cursor = database.rawQuery("SELECT matchesbet._id, hometeam, awayteam, gamble " + "FROM matchesbet INNER JOIN gambles ON matchesbet.idmatch = gambles.idmatch " + "GROUP BY matchesbet.idmatch ORDER BY matchesbet._id DESC", null);
  • 我唯一注意到的是,通过删除 GROUP BY,我将检索来自 gambles 表的 3 个元素。但是一旦我显示我在列表中有 3 个元素。我想要实现的是一个 List 元素,并且在 ListView 中包含的 textView 中包含 gambles 表的 3 个元素。
  • 我忘记了 WHERE 条件对于从 matchsbet 表中提取正确记录是必需的。
  • 能否用一些示例数据添加两张表的布局。
  • 我希望现在更清楚一点。

标签: join android-sqlite android-cursoradapter


【解决方案1】:

我相信你想要的很简单:-

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,' - ') 会 给:-

您可能会发现此链接很有帮助Aggregate Functions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 2015-03-11
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2022-01-17
    • 2017-01-31
    相关资源
    最近更新 更多