SQL - 按流派获得评分最高的电影

时间:2013-01-12 11:12:59

标签: sql database hsqldb

我有三张桌子:

    CREATE TABLE Movie
    (
        movieId INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
        title VARCHAR(255) NOT NULL,
        moviePath VARCHAR(500) NOT NULL
    );

    CREATE TABLE Rating
    (
        rid INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
        mid INTEGER FOREIGN KEY REFERENCES Movie(movieId) ON DELETE CASCADE, 
        uid INTEGER FOREIGN KEY REFERENCES User(id) ON DELETE CASCADE,
        rating INTEGER NOT NULL, 
    );

   CREATE TABLE Genre(
       id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
       movieId INTEGER NOT NULL FOREIGN KEY REFERENCES Movie(movieId) ON DELETE CASCADE,
       genre VARCHAR(255) NOT NULL
   );

我想创建一个sql查询,它可以从最常见的类型中找到最常见的电影(带有moviepath,标题)。

有什么想法吗?

更新

结果:

| MID |   TITLE    | MOVIEPATH |
--------------------------------
|   4 | Happy days |         a |
|   4 | Happy days |         a |

4 个答案:

答案 0 :(得分:2)

如果你提供了一些匹配的样本数据真的很棒..试试吧。看看你之前的问题,这个答案已经起草了。

SQLFIDDLE DEMO

select t.mid, t.sum_rating,
m.title, m.moviepath, g.genres
from (
  select mid, 
         sum(rating) as sum_rating,
         dense_rank() over (order by 
         sum(rating) desc) as rnk
  from rating
  group by mid
) t
left join movie m
on m.movieid = t.mid
left join genre g
on g.movieid = m.movieid
where t.rnk = 1;

结果:

| MID | SUM_RATING |      TITLE | MOVIEPATH | GENRES |
------------------------------------------------------
|   4 |         37 | Happy days |         a | comedy |
|   4 |         37 | Happy days |         a | RomCom |

您可以使用此替代方法,因为HSQL不支持dense_rank

使用order by desc and top 1

查询
-- alternatively

select t.mid, t.sum_rating,
m.title, m.moviepath, g.genres
from (
  select top 1 mid, 
         sum(rating) as sum_rating
  from rating
  group by mid
  order by sum_rating desc
) t
left join movie m
on m.movieid = t.mid
left join genre g
on g.movieid = m.movieid
;

答案 1 :(得分:1)

您可以通过将子查询中的电影的所有评分相加来计算评分。另一个子查询可以计算每种类型的最高评级。通过将它们连接在一起,您只会过滤每种类型的热门电影:

select  *
from    Movie m
join    Genre g
on      g.movieId = m.movieId
join    (
        select  r.mid
        ,       sum(Rating) as SumRating
        from    Rating r
        group by
                r.mid
        ) r
on      r.mid = m.movieId
join    (
        select  g.id as gid
        ,       max(SumRating) as MaxGenreRating
        from    (
                select  r.mid
                ,       sum(Rating) as SumRating
                from    Rating r
                group by
                        r.mid
                ) r
        join    Genre g
        on      g.movieId = r.mid
        group by
                g.id
        ) filter
on      filter.gid = g.id
        and filter.MaxGenreRating = r.SumRating

答案 2 :(得分:0)

我们在哪里可以找到观看次数?使用此表格,您可以使用以下查询找到评分最高的电影:

select Movie.movieId, Movie.title, Movie.moviepath
from movie, rating, genre
where
    movie.id = rating.mid and
    movie.id = genre.movieId
order by rating desc
limit 1; // limit is used if you are using MySQL. In other databases you can use suitable sql query.

但如果您正在寻找最常见的电影中最常见的电影,您必须拥有每部电影和每个类型的视图数量。

答案 3 :(得分:0)

我可以建议你在评级上使用聚合最大值和在类型上使用分组

select max(Rating.rating) as max_rating, Genre.genre, Movie.movieId from Movie inner join Rating on Movie.movieId = Rating.mid inner join Genre on Movie.movieId = Genre.movieId group by Genre.genre;

我不太确定这会100%起作用,因为我没有尝试过,但我的想法是使用group by。它可以与count,max,min,avg等聚合一起使用。

我希望有帮助