数据库

Sqlzoo 答案 JOIN 部分

2020-03-07  本文已影响0人  kolibreath

第一部分 JOIN operation

地址

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'

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

SELECT id,stadium,team1,team2
  FROM game WHERE id = 1012

Modify it to show the player, teamid, stadium and mdate for every German goal.

SELECT player, teamid, stadium, mdate FROM game JOIN goal ON game.id=goal.matchid WHERE goal.teamid = 'GER'

两个表内连之后理解为合并成为一个表

Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'

SELECT team1, team2, player FROM game JOIN goal ON  game.id = goal.matchid WHERE player LIKE 'Mario%'

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

SELECT player,  teamid , coach, gtime FROM goal JOIN eteam ON goal.teamid = eteam.id  WHERE gtime <= 10

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 JOIN eteam ON game.team1 = eteam.id WHERE coach = 'Fernando Santos' 

List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'

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

Instead show the name of all players who scored a goal against Germany.

SELECT distinct player FROM goal JOIN game ON goal.matchid = game.id WHERE ( team1 ='GER' OR team2 = 'GER') AND goal.teamid <> 'GER'

Show teamname and the total number of goals scored.

SELECT teamname, COUNT(matchid) FROM goal JOIN eteam ON teamid = id GROUP BY teamname

Show the stadium and the number of goals scored in each stadium.

SELECT stadium, COUNT(matchid) FROM game JOIN goal ON id = matchid GROUP BY stadium

For every match involving 'POL', show the matchid, date and the number of goals scored.

SELECT matchid, mdate, COUNT(matchid) FROM game JOIN  goal ON game.id = goal.matchid WHERE team1 = 'POL' OR team2 = 'POL' GROUP BY matchid 

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

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;

List the films where the yr is 1962 [Show id, title]

SELECT id, title FROM movie WHERE yr = 1962

Give year of 'Citizen Kane'.

SELECT yr FROM movie WHERE title = 'Citizen Kane'

List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Star Trek in the title). Order results by year.

SELECT id, title, yr FROM movie WHERE title LIKE '%Star Trek%' ORDER BY yr
上一篇下一篇

猜你喜欢

热点阅读