评分最高的二十部电影(条件:电影应该被至少40位用户评分/观看)?这是我的查询,但它没有给出正确的结果。 该数据库包含3个表:
`select Title
from Movie m
JOIN
(select MovieID, Rating
from Ratings
order by
Rating)
as r on
m.MovieID = r.MovieID
limit 20;`
你能建议正确的查询吗? This image contains the description of tables. It has 3 tables: Movie, Ratings, Users
答案 0 :(得分:0)
SQL ORDER BY关键字。 ORDER BY关键字用于按升序或降序对结果集进行排序。 ORDER BY关键字默认按升序对记录进行排序。要按降序对记录进行排序,请使用DESC关键字。欲了解更多信息,请参阅SQL ORDER BY Keyword - W3Schools这样做并获得前20名意味着获得排名前20位的电影。
另一件事是你想要获得前20部电影,所以你可以在进行连接之前从Rating
表中获取它们。这将减少连接之前数据集的大小,从而优化查询。
考虑到这些,您的查询应该是:
select Title from Movie m JOIN
(select MovieID, Rating from Ratings order by Rating DESC limit 20) as r
on m.MovieID = r.MovieID;
答案 1 :(得分:0)
IN SQL
select Title from Movie m
inner join (select top 20 MovieID,sum(Rating) Rate from Ratings group by movieid having count(UserID)>39 order by sum(Rating) DESC) tbl
on m.MovieID=tbl.MovieID
order by tbl.Rate desc
IN MYSQL
select Title from Movie m
inner join (select MovieID,sum(Rating) Rate from Ratings group by movieid having count(UserID)>39 order by sum(Rating) DESC limit 20) tbl
on m.MovieID=tbl.MovieID
order by tbl.Rate desc