sqlzoo-The JOIN operation-solution(11\12\13小题)

最近在刷sql,发现一个不错的练习网站:sqlzoo.

基本上不会的题目在百度里粘贴复制一下都可以找到大家的解答,但是做到TheJoin operation的时候发现11,12,13小题的答案无论如何都对不了。。。后来发现,在这个网站练习的时候是可以自己选择操作平台的,在页面的最上方右上角,而我的操作平台一直是Mysql,难怪直接复制网络上的答案总不能得出正确答案。。。

为了防止有同学遇到个我一样的问题,专门把基于Mysql平台的那三道小题的答案贴出来,希望有人能搜到吧~

11.For every match involving 'POL', show the matchid, date and the number of goals scored.
SELECT matchid,mdate,count(teamid) 
FROM game inner JOIN goal ON matchid = id   
     WHERE (team1 = 'POL' OR team2 = 'POL') group by mdate,matchid 
12.For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
select matchid,mdate,count(teamid) 
FROM game inner join goal on game.id=goal.matchid 
WHERE teamid='GER' 
group by mdate,matchid  
13.
List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises.
mdateteam1score1team2score2
1 July 2012
ESP
4
ITA
0
10 June 2012
ESP
1
ITA
1
10 June 2012
IRL
1
CRO
3
...
Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2.
select mdate,team1,sum(case when teamid=team1 then 1 else 0 end) score1,team2,sum(case when teamid=team2 then 1 else 0 end) score2 
from game left join goal on matchid=id 
group by mdate,matchid,team1,team2





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值