mysql从两个单独的查询中创建选择结果

时间:2013-11-16 15:25:14

标签: mysql

我正在尝试创建一个sql查询,创建一个联赛表,其中包含团队名称,游戏,总得分和平均每场得分。

对比赛的查询是: 选择team_name, COUNT(*)AS games_played 来自fixtures GROUP BY team_name;

得分总分的查询是: 选择teamsteam_name, COUNT(*)AS total_points_gained 来自scoresplayersteamsscoresplayer_id = playersplayer_idplayersteam_name = teamsteam_name GROUP BY teamsteam_name

如何组合这两个查询以获取一个结果中的所有数据?

2 个答案:

答案 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,您可以将这两个查询(例如按团队名称)合并为一个结果。