我正在尝试创建一个sql查询,创建一个联赛表,其中包含团队名称,游戏,总得分和平均每场得分。
对比赛的查询是:
选择team_name
,
COUNT(*)AS games_played
来自fixtures
GROUP BY team_name
;
得分总分的查询是:
选择teams
。team_name
,
COUNT(*)AS total_points_gained
来自scores
,players
,teams
在scores
。player_id
= players
。player_id
和players
。team_name
= teams
。team_name
GROUP BY teams
。team_name
如何组合这两个查询以获取一个结果中的所有数据?
答案 0 :(得分:1)
这应该有效:
SELECT teams.team_name, COUNT(*) AS total_points_gained,
(SELECT COUNT(*) FROM fixtures WHERE fixtures.team_name = teams.team_name) AS games_played
FROM scores,players,teams
WHERE scores.player_id=players.player_id
AND players.team_name = teams.team_name
GROUP BY teams.team_name
答案 1 :(得分:0)
您应该阅读有关JOIN的信息。通过JOIN,您可以将这两个查询(例如按团队名称)合并为一个结果。