sql:join和group by

Ela*_*nda 4 sql

有3个表:

电影(id,title,yr,score,votes,director)actor(id,name)cast(movieid,actorid,ord)

问:"约翰特拉沃尔塔"最繁忙的几年.显示他每年制作的电影数量.

答:我的尝试是语法上的.为什么?

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'
Run Code Online (Sandbox Code Playgroud)

Boh*_*ian 6

  • 您之后缺少第二个表名 join
  • 使用wherehaving

试试这个:

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 
Run Code Online (Sandbox Code Playgroud)

另请注意我使用的一致格式.如果使用良好的格式,则更容易发现语法错误

FYI having用于聚合函数,例如having count(*) > 3