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

时间:2016-11-25 20:41:39

标签: android sqlite android-cursor

我有两张桌子,让他们称他们为比赛和队友。 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

我的列表中的每个项目都包含time,team1_name和team2_name。由光标填充。它看起来像这样:

List Item Image

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

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

有关如何去做的任何提示? 感谢。

1 个答案:

答案 0 :(得分:2)

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

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)