查找他们的粉丝少于该特定类型的粉丝平均数的电影
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
答案 0 :(得分: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