0

查找其粉丝少于该特定类型的平均粉丝数量的电影

fan (fID: integer, fName: string)
movies(mID: integer, mName: string, mGenre: string, mYear: integer)
like(fID: integer, mID: integer)

外键:

(like.fID -> fan.fID)
(like.mID-.> movie.mID)

我尝试这样做以获得每部电影的平均粉丝数量,但没有奏效:

SELECT mGenre, AVG(total)
FROM findAverage, movies
GROUP BY mGenre;

CREATE VIEW findAverage AS
SELECT m.mName, COUNT(DISTINCT L.fID) as total
FROM like L, movies m
WHERE L.mID = B.mID 
GROUP BY m.mName
4

1 回答 1

1

您需要更改视图以包含该mId字段。然后你可以使用这个字段将JOIN你的视图返回到movies表中。

CREATE VIEW findAverage AS
SELECT L.mId, m.mName, COUNT(DISTINCT L.fID) as total
FROM like L
    INNER JOIN movies m ON L.mID = B.mID 
GROUP BY L.mId, m.mName

然后得到每个流派的平均值:

SELECT m.mGenre, AVG(f.total)
FROM findAverage f
    INNER JOIN movies m ON f.mId = m.mId
GROUP BY m.mGenre
于 2013-07-31T01:58:59.133 回答