美文网首页
157、MySQL入门(七):More JOIN operati

157、MySQL入门(七):More JOIN operati

作者: 陈容喜 | 来源:发表于2018-04-27 21:48 被阅读0次

Sqlzoo习题练习:More JOIN operations

习题链接:<u>http://sqlzoo.net/wiki/More_JOIN_operations</u>

下面会涉及到更多连接的概念。数据库由三个表组成:movie , actor 和 casting以及三个表之间的关系。 1.数据库表格.png

下面为More JOIN 习题内容:

--#1
/*
List the films where the yr is 1962 [Show id, title]
*/
SELECT id, title
FROM movie
WHERE yr=1962;

--#2
/*
Give year of 'Citizen Kane'. 
*/
SELECT yr 
FROM movie
WHERE title = 'Citizen Kane';

--#3
/*
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%';

--#4
/*
What id number does the actor 'Glenn Close' have? 
*/
SELECT id FROM actor
WHERE name = 'Glenn Close';

--#5
/*
What is the id of the film 'Casablanca' 
*/
SELECT id 
FROM movie
WHERE title = 'Casablanca';

--#6
/*
Obtain the cast list for 'Casablanca'.
what is a cast list?
The cast list is the names of the actors who were in the movie.
Use movieid=11768, (or whatever value you got from the previous question) 
*/
SELECT actor.name
FROM actor JOIN casting ON (actor.id=casting.actorid)
WHERE casting.movieid = 11768;

--#7
/*
Obtain the cast list for the film 'Alien' 
*/
SELECT actor.name
FROM actor JOIN casting 
ON (actor.id=casting.actorid)
JOIN movie
ON (movie.id = casting.movieid)
WHERE movie.title = 'Alien' ;

--#8
/*
List the films in which 'Harrison Ford' has appeared 
*/
SELECT movie.title
FROM actor JOIN casting 
ON (actor.id=casting.actorid)
JOIN movie
ON (movie.id = casting.movieid)
WHERE actor.name = 'Harrison Ford';

--#9
/*
List the films where 'Harrison Ford' has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role] 
*/
SELECT movie.title
FROM actor JOIN casting 
ON (actor.id=casting.actorid)
JOIN movie
ON (movie.id = casting.movieid)
WHERE actor.name = 'Harrison Ford' AND casting.ord != 1;

--#10
/*
List the films together with the leading star for all 1962 films. 
*/
SELECT movie.title,actor.name
FROM actor JOIN casting 
ON (actor.id=casting.actorid)
JOIN movie
ON (movie.id = casting.movieid)
WHERE movie.yr = 1962
AND casting.ord = 1;

知识点:MAX() 函数
MAX 函数返回一列中的最大值。NULL 值不包括在计算中。
MAX() 语法
SELECT MAX(column_name) FROM table_name

--#11
/*
Which were the busiest years for 'John Travolta', show the year and the number of movies he made each year for any year in which he made more than 2 movies. 
*/
SELECT yr,COUNT(title) FROM
  movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
where name='John Travolta'
GROUP BY yr
HAVING COUNT(title)=(SELECT MAX(c) FROM
(SELECT yr,COUNT(title) AS c FROM
   movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
 where name='John Travolta'
 GROUP BY yr) AS t
);

--#12
/*
List the film title and the leading actor for all of the films 'Julie Andrews' played in.
Did you get "Little Miss Marker twice"?
Julie Andrews starred in the 1980 remake of Little Miss Marker and not the original(1934).
Title is not a unique field, create a table of IDs in your subquery
*/
SELECT DISTINCT m.title,a.name
FROM (SELECT movie.*
      FROM actor JOIN casting 
      ON (actor.id=casting.actorid)
      JOIN movie
      ON (movie.id = casting.movieid)
      WHERE actor.name = 'Julie Andrews') AS m
JOIN (SELECT actor.*,casting.movieid AS movieid
      FROM actor JOIN casting 
      ON (actor.id=casting.actorid)
      WHERE casting.ord = 1) AS a
ON m.id = a.movieid
ORDER BY m.title;
 
--#13
/*
Obtain a list, in alphabetical order, of actors who've had at least 30 starring roles. 
*/
SELECT actor.name
FROM actor
JOIN casting
on (actor.id = casting.actorid)
WHERE casting.ord = 1
GROUP BY actor.name
HAVING COUNT(*) >= 30;

--#14
/*
List the films released in the year 1978 ordered by the number of actors in the cast, then by title. 
*/
SELECT movie.title, COUNT(actorid) AS cast_actors
FROM movie
JOIN casting
ON movie.id = casting.movieid
JOIN actor
ON actor.id = casting.actorid
WHERE movie.yr = 1978
GROUP BY movie.title
ORDER BY cast_actors DESC,movie.title;

--#15
/*
 List all the people who have worked with 'Art Garfunkel'. 
*/
SELECT a.name
  FROM (SELECT movie.*
          FROM movie
          JOIN casting
            ON casting.movieid = movie.id
          JOIN actor
            ON actor.id = casting.actorid
         WHERE actor.name = 'Art Garfunkel') AS m
  JOIN (SELECT actor.*, casting.movieid
          FROM actor
          JOIN casting
            ON casting.actorid = actor.id
         WHERE actor.name != 'Art Garfunkel') as a
    ON m.id = a.movieid;

相关文章

网友评论

      本文标题:157、MySQL入门(七):More JOIN operati

      本文链接:https://www.haomeiwen.com/subject/fvoilftx.html