sqlzoo练习之JOIN,运行成功

2021/9/23 19:41:18

本文主要是介绍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

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 a JOIN goal b ON (a.id=b.matchid) WHERE 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 a JOIN goal b ON(a.id=b.matchid) WHERE player LIKE 'Mario%'

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

SELECT player, teamid, coach,gtime
  FROM goal a JOIN eteam b ON(a.teamid=b.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 a JOIN eteam b ON(a.team1=b.id)
WHERE coach = 'Fernando Santos'

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

SELECT player FROM game a JOIN goal b ON(a.id=b.matchid)
      WHERE stadium='National Stadium, Warsaw'

8.Instead show the name of all players who scored a goal against Germany.
找出和德国对抗的入球球员名称,联合game表和goal表,筛选条件中首先将teamid!=‘GER’,即避免结果显示德国队;另外德国队可作为team1,也可作为team2;
查询时使用DISTINCT,避免输出相同的球员名称
DISTINCT用法

SELECT distinct player 
   FROM game a JOIN goal b ON(a.id=b.matchid)
       WHERE teamid!='GER' AND (team1='GER' or team2='GER')

9.Show teamname and the total number of goals scored.
联合eteam表和goal表,队伍名称的数量即为该队入球总数,count()为聚合函数GROUP BY 语句中,select指定字段必须为分组依据字段,其他字段若想出现则必须结合聚合函数
GROUP BY用法

SELECT teamname,count(teamname) FROM goal a JOIN eteam b ON(a.teamid=b.id) 
     GROUP BY teamname
         

10.列出場館名和在該場館的入球數字。

SELECT stadium,count(stadium) FROM game a JOIN goal b ON(a.id=b.matchid)
GROUP BY stadium

11.每一場波蘭’POL’有參與的賽事中,列出賽事編號 matchid, 日期date 和入球數字。
这里的入球数字包括重复的数据

SELECT matchid.mdate,count(*) FROM game a JOIN goal b ON(a.id=b.matchid)
      WHERE (team1='POL' or team2='POL')
            GROUP BY matchid,mdate

12.For every match where ‘GER’ scored, show matchid, match date and the number of goals scored by ‘GER’
此题指定GRE德国的入球数字,与上一题有所区别

SELECT matchid, mdate,count(*) 
      FROM game a JOIN goal b 
          ON(a.id=b.matchid)
            WHERE (teamid='GER')  # 与11题不同的筛选条件
                 group by matchid,mdate

13.Sort your result by mdate, matchid, team1 and team2.
– SUM可直接对CASE WHEN进行求和,CASE WHEN本来输出的就是一个字段(列)
– 要使用外连接,因为表中有比分为0:0的,若使用内连接则不会显示这些比赛

SELECT a.mdate,a.team1, 
  SUM(CASE WHEN b.teamid=a.team1 THEN 1 ELSE 0 END) score1,
  a.team2,
  SUM(CASE WHEN b.teamid=a.team2 THEN 1 ELSE 0 END) score2
  FROM game a LEFT JOIN goal b ON b.matchid = a.id
  GROUP BY a.mdate,b.matchid,a.team1,a.team2	




这篇关于sqlzoo练习之JOIN,运行成功的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程