mysql - 电影数据库的 SQL 查询

标签 mysql sql

排名前 20 位的电影(条件:至少有 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

最佳答案

在 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

在数据库中

 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

关于mysql - 电影数据库的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47245750/

相关文章:

mysql 根据值将一列分成多列

sql - 即使分组依据中不存在,如何检索数据?

mysql转储: Got error: 1017: Can't find file: './access.frm' (errno: 13) when using LOCK TABLES

Mysql,有没有办法知道在多个条件WHERE中哪些条件返回true?

mysql - 在sql表中查找最大值

php - MySQL 错误 : Unable to jump to row 0 on MySQL result index 8

sql - PostgreSQL 检查 Golang 中的空数组更改行为

sql - 分组不同或更改为多个子查询

mysql - 使用 eloquent 从一种形式在多个模型中创建记录

php - 关于 MySQL 中的 Join 语句(或其他有效方法)的问题