如何使用JOIN语句获取SQL查询的目标结果

时间:2015-06-17 18:32:32

标签: sql

http://sqlzoo.net/wiki/The_JOIN_operation上尝试解决#12

“对于'GER'得分的每场比赛,显示比赛,比赛日期以及'GER'得分的进球数

这就是我在查询框中的内容:

SELECT matchid, mdate, COUNT(player)
FROM game 
JOIN goal 
ON game.matchid = goal.id 
WHERE (team1 = 'GER' OR team2 = 'GER')
GROUP BY matchid

我一直得到“错误答案。有些数据不正确。”你能帮我吗?谢谢。

2 个答案:

答案 0 :(得分:1)

扩展我的评论:

Select a.matchID, b.mdate, count(*) as Goals
from goal a
inner join game b
on a.matchID = b.ID
where a.teamID = 'GER'
group by a.matchID, b.mdate

你需要从GER得分的目标开始,然后加入以获得有关比赛的其他信息。你的答案从GER正在播放的游戏开始,这不是所要求的。

答案 1 :(得分:0)

好的,在APH的帮助下,我将原始代码修改为:

SELECT matchid, mdate, COUNT(player)
FROM game 
JOIN goal 
ON game.id = goal.matchid
WHERE (team1 = 'GER' OR team2 = 'GER') AND teamid = 'GER'
GROUP BY matchid

我在WHERE语句中添加了“teamid ='GER'”。 再次感谢!