我有2个表:电影和使用以下架构的演员:
movies: | id | name | score |
cast: | movie_id | cast_id | cast_name |
-- cast.movie_id corresponds with movies.id
我想在某些条件下找到平均电影分数最高的演员(评分<40的电影应该被忽略,选中的演员应该出现在> 2部电影中)。这就是我现在拥有的:
SELECT c.cast_id, c.cast_name, AVG(m.score) AS average_score FROM movies AS m
INNER JOIN
(SELECT * FROM cast GROUP BY cast_id HAVING COUNT(cast_id) > 2) AS c
ON m.id == c.movie_id
GROUP BY c.cast_id
HAVING m.score >= 40
ORDER BY AVG(m.score) DESC, c.cast_name ASC
LIMIT 10;
但是当我检查结果时,例如对于某些演员来说,输出平均分为100.0,但他的电影的分数分别为100,100,85。我可以知道上述SELECT语句出错了吗?
你可以这样说:
SELECT c.cast_id, c.cast_name, AVG(m.score) AS average_score
FROM movies m INNER JOIN
cast c
ON m.id = c.movie_id INNER JOIN
(SELECT c.cast_id
FROM cast c
GROUP BY c.cast_id
HAVING COUNT(*) > 2 -- Only need `COUNT(DISTINCT c.movie_id)` if a person can be in one movie more than once
) cc
ON cc.cast_id = c.cast_id
WHERE m.score >= 40
GROUP BY c.cast_id, c.cast_name
ORDER BY average_score DESC, c.cast_name
LIMIT 10;
笔记:
JOIN
到cast
来获取id和名字。HAVING
条款是不必要的。