1. Introduction
表结构
movie (id, title, yr, director, budget, gross)
actor (id, name)
casting (movieid, actorid, ord)
错题 | 知识点 |
---|---|
10 | 三表联查 |
2. Q & A
- 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
- What id number does the actor 'Glenn Close' have?
SELECT id
FROM actor
WHERE name = 'Glenn Close'
- What is the id of the film 'Casablanca'
SELECT id
FROM movie
WHERE title = 'Casablanca'
- Obtain the cast list for 'Casablanca'. Use movieid=11768, (or whatever value you got from the previous question)
SELECT name FROM actor
JOIN casting ON id=actorid
WHERE movieid=11768
- Obtain the cast list for the film 'Alien'
SELECT name FROM actor
JOIN casting ON id=actorid
WHERE movieid=(
SELECT id FROM movie
WHERE title='Alien'
)
- List the films in which 'Harrison Ford' has appeared
SELECT title FROM movie
JOIN casting ON id=movieid
WHERE actorid=(
SELECT id FROM actor
WHERE name='Harrison Ford'
)
- 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 title FROM movie
JOIN casting ON id=movieid
WHERE actorid=(
SELECT id FROM actor
WHERE name='Harrison Ford')
and ord!=1
- List the films together with the leading star for all 1962 films.
- 错题
SELECT movie.title,actor.name FROM
movie JOIN casting ON movie.id=casting.movieid
JOIN actor ON actor.id=casting.actorid
WHERE yr=1962 AND casting.ord=1
- 困难:三表联查的逻辑没想通,在后续训练中一定要注意理清查询逻辑.
- Which were the busiest years for 'Rock Hudson', 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='Rock Hudson'
GROUP BY yr
HAVING COUNT(title) > 2
- List the film title and the leading actor for all of the films 'Julie Andrews' played in.
- 思路:
casting
和actor
两表联查出'Julie Andrews'参演过的movieid
,然后再联合表movie
查出电影名称和主演.
SELECT movie.title,actor.name FROM
movie JOIN casting ON movie.id=movieid
JOIN actor ON actorid=actor.id
WHERE movie.id in
(SELECT movieid FROM
casting JOIN actor ON actor.id = actorid
WHERE actor.name='Julie Andrews')
and casting.ord=1
- Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.
- 思路:先在表
casting
中查出主演过至少15部电影的actorid
(即内层子查询),然后根据演员id在表actor
中查演员姓名。也可不用子查询,得到actorid
后联查表actor
也可得到演员姓名。
SELECT name FROM actor
WHERE id IN(
SELECT actorid FROM casting
WHERE ord=1
GROUP BY actorid
HAVING count(movieid)>=15
)
ORDER BY name
- List the films released in the year 1978 ordered by the number of actors in the cast, then by title.
SELECT title,count(actorid) FROM movie
JOIN casting ON movie.id=casting.movieid
WHERE yr=1978
GROUP BY movieid
ORDER BY count(actorid) desc,title
- List all the people who have worked with 'Art Garfunkel'.
- 思路:
1)在表actor
中查出名为'Art Garfunkel'的演员id
2)在表casting
中根据actorid
查出这样演员参演的电影movieid
3)在表casting
中根据movieid
查出参演这些电影的演员id
4)回到表actor
根据演员id查出演员姓名.
SELECT name FROM actor
WHERE id IN(
SELECT actorid FROM casting
WHERE movieid IN(
SELECT movieid FROM casting
WHERE actorid IN(
SELECT id FROM actor
WHERE name='Art Garfunkel'
)
)
AND name<>'Art Garfunkel'
)
- 但是这个套娃太绕了,换成联查.
SELECT name FROM actor
JOIN casting ON actor.id=casting.actorid
WHERE movieid IN (
SELECT movieid FROM
casting JOIN actor on (actor.id = actorid)
WHERE name = 'Art Garfunkel'
) AND name <> 'Art Garfunkel'