好的 - 我有三张桌子,结构如下
tbl_1
------
userid
teamid
teamname
eliminated
tbl_2
------
teamid
week
team
tbl_3
------
team
NFLname
nfl-schedule
------
week
time
awayteam (same as "team" in tbl_3)
hometeam (same as "team" in tbl_3)
这是对“幸存者联盟”的查询,我需要每周获得队友,队名,淘汰赛,球队,周和NFL名。如果用户没有选择本周的团队,例如第2周,我希望看到该周的空白行。我假设我可以将每个teamid的行回填到数据库中,但是想知道我是否可以使用sql和一些内部联接来完成这个操作?
select a.teamid, a.teamname, a.eliminated, b.team, b.week, c.NFLnamefrom `tbl_1` a
left join `tbl_2` b on a.teamid = b.teamid
left join `tbl_3` c on c.`team` = b.team
where a.userid = XXX
答案 0 :(得分:0)
您可以使用左连接,但您必须处理使用表的顺序。 另一个问题是本周有另一个用户的选择,所以我认为你必须使用这样的子查询:
select w.week,t.teamid,t.teamname,t.eliminated,t.team,t.NFLname
from nfl-schedule w
left join (
select a.teamid, a.teamname, a.eliminated, b.team, b.week, c.NFLname
from `tbl_1` a
left join `tbl_2` b on a.teamid = b.teamid
left join `tbl_3` c on c.`team` = b.team
where a.userid = XXX
) t on w.week = t.week