SQLZOO——The JOIN operation

目录

1.Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: teamid = 'GER'

2.Show id, stadium, team1, team2 for just game 1012

3.The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.Modify it to show the player, teamid, stadium and mdate for every German goal.

4.Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'

 5.Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10

 6.List the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.

7.List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'

 8.Instead show the name of all players who scored a goal against Germany

9.Show teamname and the total number of goals scored

10.Show the stadium and the number of goals scored in each stadium.

11.For every match involving 'POL', show the matchid, date and the number of goals scored.

 12.For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'

13.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


1.Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: teamid = 'GER'

SELECT matchid, player
FROM goal
WHERE teamid = 'GER'

2.Show id, stadium, team1, team2 for just game 1012

SELECT id, stadium, team1,team2
FROM game
WHERE id=1012
--明明很简单一道题,不知道为什么题目那么长......

3.The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.Modify it to show the player, teamid, stadium and mdate for every German goal.

SELECT player, teamid, stadium, mdate
FROM game JOIN goal ON (id=matchid)
WHERE goal.teamid = 'GER'

4.Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'

SELECT team1, team2, player
FROM game JOIN goal ON (id=matchid)
WHERE player LIKE 'Mario%'
--直接联结id,mathchid即可

5.Show playerteamidcoachgtime for all goals scored in the first 10 minutes gtime<=10

SELECT player, teamid, coach, gtime
FROM goal JOIN eteam ON teamid = game.id
WHERE gtime <= 10

6.List the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.

SELECT mdate, teamname
FROM game JOIN eteam ON (team1 = esteam.id)
WHERE coach = 'Fernando Santos'
--题目要求的coach是team1,因此,在联结时,一定是team1 = eteam.id

7.List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'

SELECT player
FROM goal JOIN game ON game.id = matchid
WHERE stadium = 'National Stadium, Warsaw'
--别忘了逗号

8.Instead show the name of all players who scored a goal against Germany

SELECT DISTINCT(player)
FROM game JOIN goal ON (matchid = game.id)
WHERE (team1='GER' OR team2='GER')
AND goal.teamid != 'GER'
--注意题目预先给定的是'GRE'!!!   Germany是'GER'

9.Show teamname and the total number of goals scored

SELECT teamname, COUNT(*)
FROM eteam JOIN goal ON eteam.id=teamid
GROUP BY teamname
--COUNT分组里面的列

10.Show the stadium and the number of goals scored in each stadium.

SELECT stadium, COUNT(*)
FROM game JOIN goal ON (game.id = matchid)
GROUP BY stadium
--题目要求的应该是计算每个stadium比赛次数 

11.For every match involving 'POL', show the matchid, date and the number of goals scored.

SELECT matchid, mdate, COUNT(teamid)
FROM game JOIN goal ON matchid = game.id 
WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY matchid, mdate
--分组(SELECT出来的需要分组,COUNT teamid最后不需要分了)之后再计数,只要涉及POL得分的都需要COUNT

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 JOIN goal ON (matchid = game.id)
WHERE teamid = 'GER'
GROUP BY matchid, mdate
--与上一题类似,不同的是本题直接筛选出GER得分行即可

13.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 ) AS score1,
  team2,
  SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END ) AS score2
FROM game LEFT JOIN goal ON matchid = game.id
GROUP BY mdate, matchid, team1, team2
--不要忘掉逗号、分组
--LEFT JOIN 左边表的id一定都要有
--JOIN 查询两个有相同ID的数据

转载:图解LEFT JOIN、RIGHT JOIN 、JOIN的区别

网址1     https://blog.csdn.net/GXL_1012/article/details/83626925

网址2     https://segmentfault.com/a/1190000017369618

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值