自己总是记不住事情,所以记录下自己思考的过程,免得以后忘记了!!
The JOIN operation
跟着sqlzoo的引导,开始学会使用Join操作多张表,链接如下:sqlzoo的JOIN篇
1.
The first example shows the goal scored by a player with the last name ‘Bender’. The * says to list all the columns in the table - a shorter way of saying matchid, teamid, player, gtime
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.
From the previous query you can see that Lars Bender’s scored a goal in game 1012. Now we want to know what teams were playing in that match.
Notice in the that the column matchid in the goal table corresponds to the id column in the game table. We can look up information about game 1012 by finding that row in the game table.
Show id, stadium, team1, team2 for just game 1012
思考过程:stadium、team1、team2都是game表中信息,单表查询锁定条件id是1012即可
SELECT id,stadium,team1,team2
FROM game where id = 1012
3.
You can combine the two steps into a single query with a JOIN.
SELECT *
FROM game JOIN goal ON (id=matchid)
The FROM clause says to merge data from the goal table with that from the game table. The ON says how to figure out which rows in game go with which rows in goal - the matchid from goal must match id from game. (If we wanted to be more clear/specific we could say
ON (game.id=goal.matchid)
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.
关键:选择每场德国队进球的数据,包括进球球员、所在队伍、比赛场地、比赛日期
思路:
- 题目规定的其他数据在一张表上找不到,按照id和matchid两个属性关联
game
表和goal
表 - 条件锁定德国队,teamid=‘Ger’
SELECT player, teamid, stadium, mdate
FROM game inner JOIN goal ON (game.id=goal.matchid) where teamid='Ger'
重要的区分Ger和Gre两个缩写之前的区别!!!,前面一直被卡在这里了,就是没有搞清楚这个区别…
4
Use the same JOIN as in the previous question.
Show the team1, team2 and player for every goal scored by a player called Mario playerLIKE 'Mario%'
select team1, team2, player
from game join goal on (game.id=goal.matchid)
where player LIKE 'Mario%'
理解题目意思:
- 先锁定题目需要提取的信息是team1,team2以及player,前两个在
game
表,player在goal
表
-> Join关联两个表 - 每场得分的人是以
Mario
名称开头的,用Like和通配符锁定名字是Mario开头的
select team1, team2, player
from game join goal on (game.id=goal.matchid)
where player LIKE 'Mario%'
5
The table eteam gives details of every national team including the coach. You can JOIN goal to eteam using the phrase goal JOIN eteam on teamid=id
Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10
- 锁定题目要求选择信息是player,teamid,coach,gtime;其中coach在
eteam
表,其余信息来自goal
表 - 两张表的关联信息是teamid和id,使用join通过这两个信息关联
- 筛选条件锁定gtime<=10
SELECT player, teamid, coach, gtime
from goal join eteam on(teamid=id) where gtime <=10
6
To JOIN game with eteam you could use either
game JOIN eteam ON (team1=eteam.id)
orgame JOIN eteam ON (team2=eteam.id)
Notice that because id is a column name in both game and eteam you must specify eteam.id instead of just id
List the dates of the matches and the name of the team in which ‘Fernando Santos’ was the team1 coach.
思考问题
- matchid以及teamname可以在
game
表里面找到,coach信息可以在eteam
表里面找到->关联game和eteam表 - 筛选条件:coach= ‘Fernando Santos’
select mdate, teamname
from game join eteam on (team1 = eteam.id)
where coach='Fernando Santos'
7.
List the player for every goal scored in a game where the stadium was ‘National Stadium, Warsaw’
理解问题:
输出:player
条件:在National Stadium, Warsaw的比赛进过球的player.
思考实现过程
- player信息出现在
goal
表,stadium出现在game
表,所以需要链接goal
和game
表 - 条件是stadium=‘National Stadium, Warsaw’
select player
from game join goal on (matchid=id)
where stadium='National Stadium, Warsaw'
8
The example query shows all goals scored in the Germany-Greece quarterfinal.
Instead show the name of all players who scored a goal against Germany.
关注最后一句话就是解题的关键
思考过程:againist Germany是解题关键
- 要得到对阵德国队的球队进球信息。意味着德国队是比赛主角之一,也就是说
game
表中team1或者team2得是德国队 - player信息在
goal
表中,按照matchid和id关联以后,就可以得到有德国队的比赛得分记录 - 得分有德国队的得分也可能是其他队的得分,筛选条件补充teamid != ‘GER’
SELECT distinct player
FROM game JOIN goal ON matchid = id
WHERE (team1='GER' or team2 ='Ger') and teamid != "GER"
9
Show teamname and the total number of goals scored.
理解题目
输出:球队名称 和 总的进球数
思考:
- 球队名称 在
eteam
表,进球数在goal
表 -> 关联eteam
表和goal
表 - 统计总得分使用count命令,并且使用group by分组统计,否则只有一条记录。
SELECT teamname, count(teamid)
FROM eteam JOIN goal ON id=teamid
group by teamname
10
Show the stadium and the number of goals scored in each stadium.
理解题目:
- 选择stadium,以及每个stadium上的得分统计
思考过程: - stadium是在
game
表上,得分在goal
表上->关联两张表 - goal表上每条记录代表的是进一个球,得了一分,那么也就意味着关联两张表之后,如果按照分组group by stadium,就得到了stadium的得分分组,并且再按照count聚合函数以后,就能统计出来每个stadium上总得分。
select stadium, count(matchid)
from game join goal on (matchid=id)
group by stadium
11
For every match involving ‘POL’, show the matchid, date and the number of goals scored.
理解题目:
每场包含POL这个队伍的比赛
思考过程
输出:matchid,date,goals得分统计
- date可以再game表上找到,matchid和得分统计需要在goal上找到,关联game和goal表
- 包含POL比赛,意味着game表上的team1或者team2,有一个是POL
- 按照matchid分组统计总得分
SELECT matchid,mdate,count(matchid) as goalnumber
FROM game JOIN goal ON matchid = id
WHERE (team1 = 'POL' OR team2 = 'POL') group by matchid
12
For every match where ‘GER’ scored, show matchid, match date and the number of goals scored by ‘GER’
理解题目:
德国队得过分的比赛信息
思考过程:
- matchid和goals出现在goal表,date出现在game表,关联game和goal表
- 筛选条件需要加上teamid=GER,并且对关联表按得分分组和count计数,同上题原理
select matchid, mdate, count(matchid)
from game join goal on matchid=id
where teamid='GER' group by 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.
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.
理解题目:
要求输出5列表,类似于比赛记录板,左边是队伍A,右边是队伍B,统计得分A:B
思考过程:
1.输出5列表,其中mdate、team1、team2在game表,得分记录则是在goal表上,所以需要关联game和goal表
2.统计A和B的得分,就需要用到题目提示的case when语句,因为同前面所说,goal表每一条记录代表了一分,因此采用sum方式统计得分,并将这个得分输出score1和score2。
SELECT gm.mdate,
gm.team1,
sum(CASE WHEN gl.teamid=gm.team1 THEN 1 ELSE 0 end ) as score1,
gm.team2,
sum(CASE WHEN gl.teamid=gm.team2 THEN 1 ELSE 0 end ) as score2
FROM game gm left JOIN goal gl ON (gm.id = gl.matchid)
group by gm.mdate, gl.matchid, gm.team1, gm.team2