MovieInfo (mvID, title, rating, year, length, studio)
DirectorInfo(directorID, firstname, lastname)
MemberInfo(username, email, password)
ActorInfo(actorID, firstname, lastname, gender, birthplace)
CastInfo(mvID*, actorID*)
DirectInfo(mvID*, directorID*)
GenreInfo(mvID*, genre)
RankingInfo(username*, mvID*, score, voteDate)
我需要获得拥有最多喜剧电影的导演。 (我还需要使用ALL运算符)。我的理解是获取mvid where genre = 'Comedy" and directorid
:
select mvid
from genreinfo
where genre = 'Comedy'
union all
select directorid
from directorinfo
;
但是,我如何计算特定导演的电影数量?我怎样才能获得“喜剧”电影数量最多的单曲?
答案 0 :(得分:3)
你走在正确的轨道上。我建议查看JOIN
s。
我已就如何获得所需结果提供了逐步解答。如果您只想要最终查询,请转到步骤5并选择适合您的DBMS的那个。
SELECT mvid
FROM GenreInfo
WHERE genre = 'Comedy';
SELECT directorID
FROM DirectInfo
JOIN GenreInfo
ON DirectInfo.mvID = GenreInfo.mvID
WHERE genre = 'Comedy';
SELECT firstname
FROM DirectorInfo
JOIN DirectInfo
ON DirectorInfo.directorID = DirectInfo.directorID
JOIN GenreInfo
ON DirectInfo.mvID = GenreInfo.mvID
WHERE genre = 'Comedy';
SELECT firstname, COUNT(*) AS NumberOfMovies
FROM DirectorInfo
JOIN DirectInfo
ON DirectorInfo.directorID = DirectInfo.directorID
JOIN GenreInfo
ON DirectInfo.mvID = GenreInfo.mvID
WHERE genre = 'Comedy'
GROUP BY DirectorInfo.directorID;
SELECT firstname, COUNT(*) AS NumberOfMovies
FROM DirectorInfo
JOIN DirectInfo
ON DirectorInfo.directorID = DirectInfo.directorID
JOIN GenreInfo
ON DirectInfo.mvID = GenreInfo.mvID
WHERE genre = 'Comedy'
GROUP BY DirectorInfo.directorID
ORDER BY NumberOfMovies
LIMIT 1;
如果您使用的是SQL Server,请改用TOP
:
SELECT TOP 1 firstname, COUNT(*) AS NumberOfMovies
FROM DirectorInfo
JOIN DirectInfo
ON DirectorInfo.directorID = DirectInfo.directorID
JOIN GenreInfo
ON DirectInfo.mvID = GenreInfo.mvID
WHERE genre = 'Comedy'
GROUP BY DirectorInfo.directorID
ORDER BY NumberOfMovies;
答案 1 :(得分:0)
您可以使用联接和分组来获取结果。
select DirectorID,COUNT(mvid)
from DirectInfo d
inner join genreinfo g
ON d.mvid=g.mvid
where genre ='Comedy'
GROUP BY DirectorID
ORDER BY COUNT(mvid)
答案 2 :(得分:0)
这是作业吗?那么,你现在正在选择一个ID列表,其中一些代表导演,另一些代表电影。你注意到这根本不是你应该做的,对吧?
你想要的是导演名单。所以你从DirectorInfo表中选择。您还需要有关他的电影的信息(兴奋地:某种电影的数量)。所以你必须从MovieInfo加入这些信息。现在想想你还有什么需要粘在一起从导演到他们的电影。然后想想如何粘贴那种类型的标准。
将所有内容加入后,您就会对结果进行分组。你想要每个导演一个记录(而不是每个导演和电影的记录),所以你组成一个小组并在该组内计算。
我希望这有助于您解决任务。祝你好运!
答案 3 :(得分:-1)
select di.directorid, count(1) as 'no_of_comedy_movies'
from DirectorInfo di inner join join DirectInfo dri
on di.directorid = dri.directorid
inner join genreinfo gi
on gi.mvid = dri.mvid
where gi.genre = 'Comedy'
group by dri.directorID
order by no_of_comedy_movies