有3张 table :
movie(id, title, yr, score, votes, director) actor(id, name) casting(movieid, actorid, ord)
Q:Which were the busiest years for 'John Travolta'. Show the number of movies he made for each year.
答:我的尝试在语法上很旧。为什么 ?
select yr, count(*)
from
(actor join casting
on (actor.id = casting.actorid)
join
on (movie.id = casting.movieid)
group by yr
having actor.name='John Travolta'
最佳答案
join
之后的第二个表名where
不是 having
尝试这个:
select yr, count(*)
from actor
join casting on actor.id = casting.actorid
join movie on movie.id = casting.movieid -- you were missing table name "movie"
where actor.name='John Travolta' -- "where", not "having"
group by yr
还要注意我使用的一致格式。如果使用好的格式,更容易发现语法错误
仅供引用,
having
用于聚合函数,例如 having count(*) > 3
关于sql : join and group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6386374/