标签:
This tutorial introduces the notion of a join. The database consists of three tables movie
, actor
and casting
.
movie | actor | casting |
---|---|---|
id | id | movieid |
title | name | actorid |
yr | ord | |
director | ||
budget | ||
gross | ||
SELECT id, title FROM movie WHERE yr=1962
select yr from movie where title=‘Citizen Kane‘
select id,title,yr from movie where title like ‘%Star Trek%‘ order by yr
select title from movie where id in (11768,11955,21191)
select id from actor where name=‘Glenn Close‘
select id from movie where title=‘Casablanca‘
Obtain the cast list for ‘Casablanca‘.
The cast list is the names of the actors who were in the movie.
Use movieid=11768, this is the value that you obtained in the previous question.
select name from actor inner join casting on actor.id=casting.actorid and movieid=11768
select name from actor inner join movie on title=‘Alien‘ inner join casting on actor.id=casting.actorid and movie.id=casting.movieid
select title from movie inner join actor on name=‘Harrison Ford‘ inner join casting on movie.id=casting.movieid and actor.id=casting.actorid
select title from movie inner join actor on name=‘Harrison Ford‘ inner join casting on movie.id=casting.movieid and actor.id=casting.actorid and ord!=1
select movie.title,actor.name from movie inner join actor on yr=1962 inner join casting on movie.id=casting.movieid and actor.id=casting.actorid and ord=1
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 )
SELECT DISTINCT(title),name FROM movie inner join casting on movie.id=movieid inner join actor on actorid=actor.id and ord=1 WHERE movieid IN ( SELECT t2.movieid FROM actor t1 inner join casting t2 on t2.actorid=t1.id and name=‘Julie Andrews‘)
SELECT name FROM casting JOIN actor ON actorid = actor.id WHERE ord=1 GROUP BY name HAVING COUNT(movieid)>=30
select title,count(actorid) from movie inner join casting on movie.id=movieid and yr=1978 group by title order by 2 desc
select name from actor inner join casting on actor.id=casting.actorid and casting.movieid in (select movieid from actor inner join casting on actor.id=casting.actorid and name=‘Art Garfunkel‘) and name!=‘Art Garfunkel‘
sqlzoo练习答案--More JOIN operations
标签:
原文地址:http://blog.csdn.net/crazy__chen/article/details/50514100