sqlzoo 第 12 题, 查找某演员出演电影次数最多的年份 https://sqlzoo.net/wiki/More_JOIN_operations/zh
我这么写对吗?为啥答案搞的这么绕,性能更好吗,还是为了用 join 而用?
这是我写的
select yr, count(*) as c from movie
where id in (
select movieid from casting
where actorid=(select id from actor where name='John Travolta')
)
group by yr order by c desc limit 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
)