lantionzy
五:How to do joinshttp://sqlzoo.cn/3b.htm
    1a. Show the athelete (who) and the country name for medal winners in 2000
        SELECT who, country.name FROM ttms 
      JOIN country ON (ttms.country=country.id) WHERE games = 2000
    1b. Show the who and the color of the medal for the medal winners from \'Sweden\'
        SELECT who, color FROM ttms 
      JOIN country ON (ttms.country=country.id) WHERE country.name = \'Sweden\'
    1c. Show the years in which \'China\' won a \'gold\' medal
        SELECT games FROM ttms JOIN country ON (ttms.country = country.id)
      WHERE name = \'China\' AND color = \'gold\'
    2a. Show who won medals in the \'Barcelona\' games
        SELECT who FROM ttws JOIN games ON (ttws.games=games.yr) WHERE city = \'Barcelona\'
    2b. Show which city \'Jing Chen\' won medals. Show the city and the medal color
        SELECT city,color FROM games JOIN ttws ON( ttws.games=games.yr) WHERE who = \'Jing Chen\'
    2c. Show who won the gold medal and the city
        SELECT who, city FROM ttws JOIN games ON(ttws.games = games.yr) WHERE color = \'gold\'
    3a. Show the games and color of the medal won by the team that includes \'Sen Yan\'
        SELECT games,color FROM ttmd JOIN team ON(ttmd.team = team.id) WHERE team.name = \'Sen Yan\'
    3b. Show the \'gold\' medal winners in 2004
        SELECT name FROM team JOIN ttmd ON(ttmd.team = team.id) WHERE color = \'gold\' AND games = 2004
    3c. Show the name of each medal winner country \'FRA\'
        SELECT name FROM team JOIN ttmd ON(ttmd.team = team.id) WHERE country = \'FRA\'

六:The JOIN operationhttp://sqlzoo.cn/3.htm
    1a. 列出获得不少于30000 votes(选票)的电影. [显示 title, votes
        SELECT title, votes FROM movie WHERE votes>=30000
    1b. 电影\'Citizen Kane\'的首映年份
        SELECT yr FROM movie WHERE title = \'Citizen Kane\'
    1c. 列出包含the Police Academy(警校)字样的title(电影名称)和 score(得分) films. [The films have a title that is LIKE \'Police Academy%\'
        SELECT title,score FROM movie Where title LIKE \'Police Academy%\'
    1d. 列出所有the Star Trek movies(星际系列电影),显示title(电影标题)和score(得分). 按电影的发行 yr(年份)排序
        SELECT title,score FROM movie WHERE title LIKE \'%Star Trek%\' ORDER BY yr ASC
    1e. 列出名称中包含\'Dog\'的电影名和得分
        SELECT title,score FROM movie WHERE title LIKE \'%Dog%\'
    2a. 列出id为 1, 2, 3的电影的名称
        SELECT title FROM movie WHERE id in (1,2,3)
    2b. 电影\'Glenn Close\' 的ID号是多少? 
        SELECT id FROM actor WHERE name= \'Glenn Close\'
    2c. 电影\'Casablanca\' 的ID号是多少? 
        SELECT id FROM movie WHERE title = \'Casablanca\'
    3a. 显示id=1的电影\'Star Wars\'(星球大战)中演员(角色)名单
        SELECT name FROM casting, actor WHERE movieid=1 AND actorid=actor.id
    or:  SELECT name FROM movie, casting, actor WHERE movie.title = \'Star Wars\' AND movie.id = casting.movieid AND casting.actorid = actor.id
    3b. 显示电影\'Alien\'的演员(角色)名单
        SELECT name FROM actor, casting, movie
WHERE movie.title = \'Alien\' AND movie.id = casting.movieid AND casting.actorid = actor.id
    3c. 显示有\'Harrison Ford\'参演的电影名称
        SELECT title FROM movie 
      JOIN actor ON actor.name = \'Harrison Ford\' 
      JOIN casting ON movie.id = casting.movieid AND casting.actorid = actor.id 
    3d. 显示有\'Harrison Ford\'参演的电影名称,但\'Harrison Ford\'在该影片中不是主角(即在角色表中不是排名第一)
        SELECT title FROM movie 
      JOIN actor ON actor.name = \'Harrison Ford\' 
      JOIN casting ON movie.id = casting.movieid AND casting.actorid = actor.id AND casting.ord != 1
    3e. 显示1962年发行的电影以及该影片中主角(即在角色表中排名第一)
        SELECT title,name FROM actor 
      JOIN movie ON movie.yr = 1962 
      JOIN casting ON movie.id = casting.movieid AND casting.actorid = actor.id AND casting.ord = 1
    4a. \'John Travolta\'哪一年最忙? 显示出他每年的出演电影数量
        SELECT yr,COUNT(title) FROM movie 
      JOIN actor ON actor.name = \'John Travolta\' 
      JOIN casting ON movie.id = casting.movieid AND casting.actorid = actor.id 
      GROUP BY yr
    4b. 列出有\'Julie Andrews\'参演的所有电影名称以及该片的主角
        SELECT title, name FROM movie, casting, actor WHERE casting.movieid = movie.id AND casting.actorid = actor.id AND casting.ord = 1 AND casting.movieid IN (SELECT movieid FROM casting, actor WHERE casting.actorid = actor.id AND actor.name = \'Julie Andrews\')
    4c. 列出那些最起码在10部电影中出任过主角的演员
        SELECT name FROM casting 
      JOIN actor ON  casting.actorid = actor.id WHERE casting.ord=1
      GROUP BY name HAVING COUNT(movieid)>=10
    or:  SELECT name FROM actor,casting,movie 
        WHERE casting.actorid = actor.id AND casting.ord = 1 AND movie.id = casting.movieid 
        GROUP BY name HAVING COUNT(casting.movieid)>=10
    4d. 按演员人数的多少依次列出1978 的电影以及演员数.人数最多排在最前
        SELECT title,count(actorid) FROM movie, casting WHERE casting.movieid = movie.id AND movie.yr = 1978
      GROUP BY title ORDER BY 2 DESC
    4e. 列出所有和\'Art Garfunkel\'合作过的演员(不要列出\'Art Garfunkel\'他自己)
        SELECT DISTINCT name FROM actor,casting WHERE actor.id = casting.actorid 
      AND casting.movieid IN (SELECT movieid FROM casting,actor 
        WHERE casting.actorid = actor.id AND actor.name = \'Art Garfunkel\')
      AND name != \'Art Garfunkel\'

sql在线练习网站(http://sqlzoo.cn)答案解析(1)
sql在线练习网站(http://sqlzoo.cn)答案解析(2)
sql在线练习网站(http://sqlzoo.cn)答案解析(4)

分类:

技术点:

相关文章: