SQL 访问:如何在没有运行 2 查询的情况下获取涉及多个表的输出?

SQL Access: how to obtain output involving multiple tables without running 2 queries?

我想找出特定年龄段(例如 20-30 岁)最流行的电影类型。我是 SQL 的新手,如果能得到任何帮助,我将不胜感激,如果这太小,我深表歉意。

此查询的相关 table 是:

FILM {FID (PK), ..., Film_Title}
MEMBER {MID (PK), ..., Date_of_Birth}
LIST {MID (FK), FID (FK)}
GENRE {GID (PK), Genre}
FILM_ACTOR_DIRECTOR_GENRE {FID (FK), ..., GID (FK)}

FILM 和 MEMBER table 应该是不言自明的,而 LIST 是 MEMBER 希望租借的一系列电影。它就像一个购物篮。每个成员只有一个列表,每个列表可以包含多部电影。 FILM_ACTOR_DIRECTOR_GENRE 包含属于每部电影的流派。每部电影只能有一种类型。

到目前为止,我已经设法得到一个显示的输出:

Genre     # People Aged 20-30
-------   -------------------
Action             5
Comedy             4
Horror             2
etc.              etc.

但是它涉及创建一个 table 然后 运行 另一个查询。有没有一种方法无需 运行 2 个单独的查询即可获得特定年龄段内最流行的类型?

我使用的 2 个查询是:

SELECT DISTINCT Genre.Genre_Name, Member.Date_of_Birth
INTO Genre_by_Age
FROM
((((Genre 
INNER JOIN Film_Actor_Director_Genre ON Genre.GID = Film_Actor_Director_Genre.GID)
INNER JOIN Film ON Film_Actor_Director_Genre.FID = Film.FID) 
INNER JOIN List ON Film.FID = List.FID)
INNER JOIN Member ON Member.MID = List.MID)
WHERE (((Member.[Date_of_Birth]) Between #4/16/1995# And #4/16/1985#));

用我想要的信息创建新的 table,并且:

SELECT Genre_Name, COUNT(*) as Number_of_People_aged_20_to_30
FROM Genre_by_Age
GROUP BY Genre_Name
ORDER BY COUNT(*) DESC;

获得如上所示的输出。

有没有办法不用 运行ning 2个单独的查询就可以得到上面的结果?感谢您的宝贵时间!

我认为这应该可行:

SELECT Genre.Genre_Name, count(Member.MID) as Number_of_People_aged_20_to_30
FROM
((((Genre 
INNER JOIN Film_Actor_Director_Genre ON Genre.GID = Film_Actor_Director_Genre.GID)
INNER JOIN Film ON Film_Actor_Director_Genre.FID = Film.FID) 
INNER JOIN List ON Film.FID = List.FID)
INNER JOIN Member ON Member.MID = List.MID)
WHERE (((Member.[Date_of_Birth]) Between #4/16/1995# And #4/16/1985#))
GROUP BY Genre.Genre_Name
ORDER BY count(Member.MID) DESC;

使用子查询怎么样?

SELECT Genre_Name, COUNT(*) as Number_of_People_aged_20_to_30
FROM (SELECT DISTINCT Genre.Genre_Name, Member.Date_of_Birth
      FROM ((((Genre 
      INNER JOIN Film_Actor_Director_Genre ON Genre.GID = Film_Actor_Director_Genre.GID)
      INNER JOIN Film ON Film_Actor_Director_Genre.FID = Film.FID) 
      INNER JOIN List ON Film.FID = List.FID)
      INNER JOIN Member ON Member.MID = List.MID)
      WHERE (((Member.[Date_of_Birth]) Between #4/16/1995# And #4/16/1985#))
     ) as t
GROUP BY Genre_Name
ORDER BY COUNT(*) DESC;