四.The JOIN
mdate | team1 | score1 | team2 | score2 |
---|---|---|---|---|
1 July 2012 | ESP | 4 | ITA | 0 |
10 June 2012 | ESP | 1 | ITA | 1 |
10 June 2012 | IRL | 1 | CRO | 3 |
... |
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 game.id=matchid
group by mdate,matchid,team1,team2.
五.More JOIN
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 actor.id=actorid
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 actor.id=actorid
WHERE name='John Travolta'
GROUP BY yr) AS s )
12.List the film title and the leading actor for all of the films 'Julie Andrews' played in.
select title,name from movie
join casting on movie.id=movieid
join actor on actorid=actor.id
and ord=1
where movieid in (select movieid from casting
join actor on actor.id=actorid
where name='Julie Andrews')
13.Obtain a list, in alphabetical order, of actors who've had at least 30 starring roles.
select name from actor
join casting on actor.id=actorid
where ord=1
group by name
having count(movieid)>=30
order by name
join casting on movie.id=movieid
where yr=1978
group by title
order by count(actorid) desc ,title
join casting on actor.id=actorid
where movieid in (select movieid from casting
join actor on actorid=actor.id
where name='Art Garfunkel')
and name!='Art Garfunkel'