【问题标题】:having trouble with sql about movie theme关于电影主题的 sql 有问题
【发布时间】:2016-02-15 03:56:33
【问题描述】:
Movie(mID int, title text, year int, director text);
Reviewer(rID int, name text);
Rating(rID int, mID int, stars int, ratingDate date);

对于每位导演,返回导演的姓名以及他们执导的在所有电影中获得最高评分的电影的名称,以及该评分的值。忽略导演为 NULL 的电影。

我被困在评分最高的部分。

select title, director
from Movie natural join Rating
where director is not NULL

我知道我真的写不出来。

【问题讨论】:

  • 显示你目前拥有的 SQL 代码。
  • 不要在这里寻求家庭作业的帮助。我们不是来代表您编写代码

标签: mysql sql


【解决方案1】:
select distinct director, title, stars
from (movie join rating using (mid)) m
where stars in (select max(stars) 
                from rating join movie using (mid) 
                where m.director = director);

【讨论】:

    【解决方案2】:

    这是使用带有 max 聚合的子查询的一个选项:

    select m.title, m.director, r.stars
    from movie m
        join rating r on m.mid = r.mid
        join (select director, max(stars) maxstars
              from rating join movie on rating.mid = movie.mid
              group by director) maxr on m.director = maxr.director
                              and r.stars = maxr.maxstars
    where m.director is not null
    

    当我意识到您需要 group by director 而不是 movie 来获得每个导演的 max(stars) 时进行了编辑。

    【讨论】:

    • 如果同一部电影有多个评分,我认为它会复制输出
    猜你喜欢
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 2011-03-17
    相关资源
    最近更新 更多