1. 程式人生 > >MySQL練習6

MySQL練習6

來源於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