sqlzoo--the join operation

The JOIN operation

TABLE game:

id(編號)mdate(日期)stadium(場館)team1(隊伍1)team2(隊伍2)
10018 June 2012National Stadium, WarsawPOLGRE
10028 June 2012Stadion Miejski (Wroclaw)RUSCZE
100312 June 2012Stadion Miejski (Wroclaw)GRECZE
100412 June 2012National Stadium, WarsawPOLRUS

goal:

matchid(賽事編號)teamid(隊伍編號)player(入球球員)gtime(入球時間)
1001POLRobert Lewandowski17
1001GREDimitris Salpingidis51
1002RUSAlan Dzagoev15
1001RUSRoman Pavlyuchenko82

eteam:

id(編號)teamname(隊名)coach(教練)
POLPolandFranciszek Smuda
RUSRussiaDick Advocaat
CZECzech RepublicMichal Bilek
GREGreeceFernando Santos
  1. 代表德国队取得进球的matchid,player

    SELECT matchid,player FROM goal 
      WHERE teamid = 'GER';
    
  2. 显示赛事1012的 id, stadium, team1, team2

    SELECT id,stadium,team1,team2
      FROM game 
      WHERE id = 1012;
    
  3. 找出每一个代表德国进球的球员名称、队名、场馆以及日期

    SELECT player,teamid,stadium,mdate
      FROM game JOIN goal ON (id=matchid)
      WHERE teamid = 'GER';
    
  4. 查询Mario的team1,team2,player信息

    SELECT team1,team2,player FROM game JOIN goal ON matchid = id
       WHERE player LIKE 'Mario%';
    
  5. 每场比赛前十分钟有进球的球员名,球队,教练以及进球时间

    SELECT player, teamid, coach, gtime
      FROM goal JOIN eteam ON teamid = id
     WHERE gtime<=10;
    
  6. Fernando Santos作为team1的教练的比赛时间及队伍名

    SELECT mdate, teamname
      FROM game JOIN eteam ON team1 = eteam.id
     WHERE coach = 'Fernando Santos';
    
  7. 在 'National Stadium, Warsaw’球馆取得过进球的球员

    SELECT player
         FROM game JOIN goal ON matchid = id
        WHERE stadium = 'National Stadium, Warsaw';
    
  8. 对阵德国的比赛中取得进球的球员名称

    SELECT DISTINCT(player)
      FROM game JOIN goal ON matchid = id 
        AND teamid != 'GER' AND (team1 = 'GER' OR team2 = 'GER');
    
  9. 列出球队名称及其进球总数

    SELECT teamname,count(player)
    From eteam left join goal
    On eteam.id=teamid
    Group by teamname
    
  10. 列出球馆名称和球馆内发生的进球数

    SELECT stadium, COUNT(player)
    FROM game LEFT JOIN goal ON id = matchid
    GROUP BY stadium;
    
  11. 波兰参与的比赛,列出其比赛编号,日期和进球数

    SELECT matchid, mdate, COUNT(player)
      FROM game JOIN goal ON matchid = id AND (team1 = 'POL' OR team2 = 'POL')
      GROUP BY matchid, mdate;
    
  12. 德国参与的比赛,列出比赛编号,日期以及德国队的进球数

    SELECT matchid, mdate, COUNT(player)
      FROM game JOIN goal ON matchid = id AND (team1 = 'GER' OR team2 = 'GER')
      AND teamid = 'GER'
      GROUP BY matchid, mdate;
    
  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.(使用case when 列出每场比赛的球队及其进球数)

    mdateteam1score1team2score2
    1 July 2012ESP4ITA0
    10 June 2012ESP1ITA1
    10 June 2012IRL1CRO3

    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;
    
JOIN Quiz测验题

game:

idmdatestadiumteam1team2
10018 June 2012National Stadium, WarsawPOLGRE
10028 June 2012Stadion Miejski (Wroclaw)RUSCZE
100312 June 2012Stadion Miejski (Wroclaw)GRECZE
100412 June 2012National Stadium, WarsawPOLRUS

goal:

matchidteamidplayergtime
1001POLRobert Lewandowski17
1001GREDimitris Salpingidis51
1002RUSAlan Dzagoev15
1001RUSRoman Pavlyuchenko82

eteam:

idteamnamecoach
POLPolandFranciszek Smuda
RUSRussiaDick Advocaat
CZECzech RepublicMichal Bilek
GREGreeceFernando Santos
  1. You want to find the stadium where player ‘Dimitris Salpingidis’ scored. (Dimitris Salpingidis 取得进球的球馆)Select the JOIN condition to use:

    game  JOIN goal ON (id=matchid)
    
  2. You JOIN the tables goal and eteam in an SQL statement.(表goal和表eteam的连接) Indicate the list of column names that may be used in the SELECT line:

    matchid, teamid, player, gtime, id, teamname, coach
    
  3. Select the code which shows players, their team and the amount of goals they scored against Greece(GRE).(显示对阵希腊取得进球的球员名,队名以及进球数)

    SELECT player, teamid, COUNT(*)
      FROM game JOIN goal ON matchid = id
     WHERE (team1 = "GRE" OR team2 = "GRE")
       AND teamid != 'GRE'
     GROUP BY player, teamid
    
  4. Select the result that would be obtained from this code:

    SELECT DISTINCT teamid, mdate
      FROM goal JOIN game on (matchid=id)
     WHERE mdate = '9 June 2012'
    
    DEN9 June 2012
    GER9 June 2012
  5. Select the code which would show the player and their team for those who have scored against Poland(POL) in National Stadium, Warsaw.(在National Stadium, Warsaw.球馆中对阵波兰时取得进球的球员及其球队)

    SELECT DISTINCT player, teamid 
       FROM game JOIN goal ON matchid = id 
      WHERE stadium = 'National Stadium, Warsaw' 
     AND (team1 = 'POL' OR team2 = 'POL')
       AND teamid != 'POL'
    
  6. Select the code which shows the player, their team and the time they scored, for players who have played in Stadion Miejski (Wroclaw) but not against Italy(ITA).(在Stadion Miejski (Wroclaw)球馆取得进球(排除对阵意大利的比赛)的球员的名称,球队以及进球时间)

    SELECT DISTINCT player, teamid, gtime
      FROM game JOIN goal ON matchid = id
     WHERE stadium = 'Stadion Miejski (Wroclaw)'
       AND (( teamid = team2 AND team1 != 'ITA') OR ( teamid = team1 AND team2 != 'ITA'))
    
  7. Select the result that would be obtained from this code:

    SELECT teamname, COUNT(*)
      FROM eteam JOIN goal ON teamid = id
     GROUP BY teamname
    HAVING COUNT(*) < 3
    
    Netherlands2
    Poland2
    Republic of Ireland1
    Ukraine2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sweeney Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值