找到平均电影长度最长的流派。 SQL

时间:2015-12-07 02:24:15

标签: mysql sql xampp

我是SQL新手,我试图在mySQL上解决这个问题。 这是找到平均电影长度最长的类型。我有这种类型&电影长度排在下面,但我真的很困惑如何去取每种类型的平均值。类型:

  • 动作,
  • 喜剧,
  • 剧情,
  • 科幻,
  • 惊悚片和
  • 冒险

获取个人类型的平均值并显示其最大平均值。非常感谢任何帮助

SELECT tblGenre.Genre, tblMovie.Length
FROM tblMovie, tblMovieGenre, tblGenre
WHERE (((tblMovie.movie_ID)=[tblMovieGenre].[Movie]) AND ((tblMovieGenre.Genre)=[tblGenre].[genre_ID]))
GROUP BY tblGenre.Genre, tblMovie.Length;

输出

Genre   Length
Action  81 
Action  97 
Action  110
Action  130
Action  136
Action  145
Action  152
Action  165
Adventure   90
Adventure   109
Adventure   111
Adventure   132
Adventure   145
Adventure   178
Adventure   179
Adventure   201
Comedy  87
Comedy  90
Comedy  97
Comedy  109
Comedy  111
Drama   80
Drama   81
Drama   90
Drama   91
Drama   93
Drama   95
Drama   102
Drama   109
Drama   111
Drama   130
Drama   132
Drama   139
Drama   151
Drama   152
Drama   161
Drama   175
Drama   194
Sci-Fi  110
Sci-Fi  136
Sci-Fi  145
Thriller    84
Thriller    91
Thriller    93
Thriller    99
Thriller    110
Thriller    151
Thriller    165

4 个答案:

答案 0 :(得分:2)

我不太了解MySQL,但如果我理解正确,这就是你想要的:

SELECT tblGenre.Genre, AVERAGE(tblMovie.Length)
FROM tblMovie, tblMovieGenre, tblGenre
WHERE (((tblMovie.movie_ID)=[tblMovieGenre].[Movie]) AND ((tblMovieGenre.Genre)=[tblGenre].[genre_ID]))
GROUP BY tblGenre.Genre
ORDER BY 2 desc
limit 1;

答案 1 :(得分:2)

也许你想要这样的东西。 要获得平均电影长度,您可以使用AVG(列)。

SELECT tblGenre.Genre, AVG(tblMovie.Length)
FROM tblMovie, tblMovieGenre, tblGenre
WHERE tblMovie.movie_ID = tblMovieGenre.Movie 
AND tblMovieGenre.Genre = tblGenre.genre_ID
GROUP BY tblGenre.Genre;

从平均电影长度获得最长的电影长度

SELECT tblGenre.Genre, tblMovie.Length
FROM tblMovie, tblMovieGenre, tblGenre
    WHERE tblMovie.movie_ID = tblMovieGenre.Movie 
    AND tblMovieGenre.Genre = tblGenre.genre_ID
    AND tblMovie.Length > (SELECT tblGenre.Genre, AVG(tblMovie.Length)
              FROM tblMovie, tblMovieGenre, tblGenre
              WHERE tblMovie.movie_ID = tblMovieGenre.Movie 
              AND tblMovieGenre.Genre = tblGenre.genre_ID
              GROUP BY tblGenre.Genre);

答案 2 :(得分:1)

我认为这就是你要找的东西:

SELECT tblGenre.Genre, AVERAGE(tblMovie.Length)
FROM tblMovie, tblMovieGenre, tblGenre
WHERE (((tblMovie.movie_ID)=[tblMovieGenre].[Movie]) AND ((tblMovieGenre.Genre)=[tblGenre].[genre_ID]))
GROUP BY tblGenre.Genre;

答案 3 :(得分:0)

 SELECT TOP 1 tblGenre.Genre, Avg(tblMovie.Length) AS AVG_Moive
 FROM tblMovie, tblMovieGenre, tblGenre
 WHERE (((tblMovie.movie_ID)=[tblMovieGenre].[Movie]) AND ((tblMovieGenre.Genre)=[tblGenre].[genre_ID]))
 GROUP BY tblGenre.Genre
 ORDER BY Max(tblMovie.Length) DESC;