Android: 2 个连接表的一个游标行

Android: One cursor row for 2 joined tables

我有 2 张桌子,我们称它们为比赛和团队。 Match 与 Team 是一对二的关系。

匹配有

_id = INTEGER PRIMARY
time = TEXT NOT NULL
team1_id = TEXT NOT NULL //references a team record
team2_id = TEXT NOT NULL //references a team record

团队有

_id = INTEGER PRIMARY
name = TEXT NOT NULL

我列表中的每一项都包含时间、team1_name 和 team2_name。由游标填充。它看起来像这样:

现在当我使用连接查询时:

Select match._id, time, team.name 
From match LEFT JOIN team
ON (team._id = match.team1_id OR team._id = match.team2_id)

我收到以下游标结果:

_id    time    name
42     6:00    Barca
42     6:00    Man City

我想要实现的是将两个团队名称放在光标的两列而不是单独的行中,以便将其映射到适配器的视图,即:

_id    time    team1    team2
42     6:00    Barca    Man City

关于如何进行的任何提示? 谢谢。

您需要进行多次连接并为列名设置别名:

SELECT match._id, 
       time, 
       t1.name AS team1_name,
       t2.name AS team2_name 
  FROM match 
  LEFT JOIN team AS t1 ON (t1._id = match.team1_id)
  LEFT JOIN team AS t2 ON (t2._id = match.team2_id)