我有一个电影评级网站的表格,如下所示:
我想写一个查询,给出最慷慨的评论家最喜欢的电影的名称。目前我已将任务分解为两部分:
- 找到评论者最喜欢的电影的名称:
SELECT DISTINCT Title FROM Movies
NATURAL JOIN Ratings
WHERE rating IN (SELECT MAX(rating) FROM Ratings)
- 找出最高平均评分:
SELECT reviewerID, AVG(rating) AS avg_rating
FROM Ratings
GROUP BY reviewerID
ORDER BY avg_rating DESC LIMIT 1
但我不确定如何连接这两个查询。使用子查询,它不起作用,因为我不能在子查询中使用LIMIT。 有谁知道如何连接这两个查询? 或者可能是一种不使用子查询的方法?
答案 0 :(得分:0)
你有没有尝试过这样的事情:
SELECT Title FROM Movies
WHERE movieID = (
SELECT movieID FROM Ratings WHERE reviewerID=(
SELECT reviewerID, AVG(rating) AS avg_rating
FROM Ratings
GROUP BY reviewerID
ORDER BY avg_rating DESC LIMIT 1
)
ORDER BY rating DESC
LIMIT 1
);
答案 1 :(得分:0)
以下是使用GROUP BY
,ORDER BY
和LIMIT 1
的解决方案。
SELECT m.Title
FROM Movies as m INNER JOIN Ratings as r USING(movieID)
GROUP BY m.movieID
ORDER BY AVG(r.rating) DESC
LIMIT 1
答案 2 :(得分:0)
SELECT m.title
FROM movies m
JOIN ratings r
ON r.movieid = m.movieid
JOIN
( SELECT reviewerID
FROM Ratings
GROUP
BY reviewerID
ORDER
BY AVG(rating) DESC
LIMIT 1
) x
ON x.reviewerid = r.reviewerid
ORDER
BY r.rating DESC
LIMIT 5;
或类似的东西