来源于zoo sql, 对于表连接的相关操作
下图是表结构, eteam为足球队表, teamname为球队名,coach为教练名
game为比赛表,mdate为比赛日期,stadium为比赛地点,team1和team2为比赛双方的球队编号,关联eteam的id
goal为进球表,matchid为比赛编号,关联game的id,teamid为进球的球队编号,关联eteam的id,player为进球者的姓名,gtime为进球的时间
练习题目:
1. 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. show the player, teamid, stadium and mdate for every German goal.
SELECT player,teamid, stadium, mdate FROM game JOIN goal ON (id=matchid) and 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 goal, game where matchid = id and 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, eteam WHERE teamid = id and gtime<=10;
6. List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.
select mdate, teamname from game, eteam where team1 = eteam.id and coach = 'Fernando Santos';
7. List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'
select player from goal, game where matchid = id and stadium = 'National Stadium, Warsaw';
8. show the name of all players who scored a goal against Germany.
SELECT distinct player FROM game JOIN goal ON matchid = id WHERE team1='GER' or team2='GRE';
9. Show teamname and the total number of goals scored.
SELECT teamname, count(player) FROM eteam JOIN goal ON id=teamid group by teamname;
10. Show the stadium and the number of goals scored in each stadium.
select stadium, count(player) from goal, game where matchid = id group by stadium;
11. For every match involving 'POL', show the matchid, date and the number of goals scored.
SELECT matchid, mdate, count(player) FROM game JOIN goal ON matchid = id WHERE team1 = 'POL' OR team2 = 'POL' group by matchid, mdate;
-- MySQL: SELECT matchid, mdate, count(player) 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'
SELECT matchid, mdate,count(matchid) FROM game JOIN goal ON matchid = id
WHERE teamid = 'GER' group by matchid, mdate; -- 同上
13. ist 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.Sort your result by mdate, matchid, team1 and team2.
mdate team1 score1 team2 score2
1 July 2012 ESP 4 ITA 0
10 June 2012 ESP 1 ITA 1
10 June 2012 IRL 1 CRO 3
select mdate, team1 , (select count(player) from goal where team1 = teamid and matchid = id) as score1, team2, (select count(player) from goal where team2 = teamid and matchid = id) as score2 from game order by mdate, id, team1, team2
创建数据表和填充数据的sql下载地址:https://download.csdn.net/download/hielw/10383267