[英]Getting movie lists and rating from two tables MySQL LEFT JOIN
I have a table 我有桌子
movies 电影
movie_id
movie_name
language
cast
ratings 等级
id
movie_id
rating
comment
I need to fetch all movies details with average and total count of ratings. 我需要获取所有电影的详细信息,其中包括评分的平均值和总数。
If there is no rating for any movie, average rating and total ratings count must be zero. 如果没有任何电影的评分,则平均评分和总评分数必须为零。
for this i tried this MySQL query: 为此,我尝试了此MySQL查询:
SELECT m.movie_id,movie_name,cast,language,AVG(rating) as average_rating,COUNT(rating) as total_rating
FROM movies m
LEFT JOIN ratings r ON m.movie_id=r.movie_id;
But this query not giving all movies lists from 'movies' table. 但是此查询未提供“电影”表中的所有电影列表。
Please help. 请帮忙。 Thank you.
谢谢。
Use group by
with movie_id
so it will differentiate your result with unique movie_id. 将
group by
与movie_id
使用,以便将其结果与唯一的movie_id区分开。
SELECT m.movie_id,m.movie_name,m.cast,m.language,AVG(r.rating) as average_rating,COUNT(r.rating) as total_rating
FROM movies m
LEFT JOIN ratings r ON m.movie_id=r.movie_id
GROUP BY m.movie_id;
You need to add GROUP BY m.movie_id 您需要添加GROUP BY m.movie_id
SELECT
`m`.`movie_id`, `m`.`movie_name`, `m`.`cast`, `m`.`language`,
AVG(`r`.`rating`) `average_rating`, COUNT(`r`.`rating`) `total_rating`
FROM
`movies` `m`
LEFT JOIN
`ratings` `r`
ON
`m`.`movie_id` = `r`.`movie_id`
GROUP BY
`m`.`movie_id`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.