SQL 访问——仅限制子查询
SQL Access — Limit subquery only
我正在尝试从电影数据库中获取与特定导演合作过的演员列表,以及这些演员出演过的电影列表。
SELECT DISTINCT A_First_Name, A_Last_Name, Film_Title
FROM
(SELECT *
FROM Actor, Film, Film_Actor_Director_Genre
WHERE Actor.AID=Film_Actor_Director_Genre.AID
AND Film.FID=Film_Actor_Director_Genre.FID)
WHERE A_First_Name IN
(SELECT A_First_Name
FROM
(SELECT *
FROM Actor, Director,
Film_Actor_Director_Genre
WHERE
Actor.AID=Film_Actor_Director_Genre.AID
AND
Director.DID=Film_Actor_Director_Genre.DID)
WHERE D_First_Name=[Director’s first name?]
OR D_Last_Name=[Director’s last name?]);
假设我想知道导演D01合作过的演员名单,示例结果是这样的:
Actor Film
----- ----
A01 F01
A01 F02
A01 F03
A01 F04
A02 F01
A02 F02
A02 F05
A02 F06
A03 F01
A04 F01
A04 F02
A04 F09
A05 F01
A05 F02
A06 F10
A07 F01
A07 F02
A07 F04
etc. etc.
然而,由于与 D01 合作过的演员名单可能非常长,我只想展示前 5 名演员(即与 D01 合作过最多电影的演员),以及与演员合作过的所有电影一直在。所以我想只限演员不限片。期望的结果是:
Actor Film
----- ----
A01 F01
A01 F02
A01 F03
A01 F04
A02 F01
A02 F02
A02 F05
A02 F06
A04 F01
A04 F02
A04 F09
A05 F01
A05 F02
A07 F01
A07 F02
A07 F04
我尝试在各种子查询中实现 SELECT DISTINCT TOP 5
语法,但它们返回了 5 个结果的奇怪组合,例如
Actor Film
----- ----
A01 F01
A01 F02
A02 F01
A02 F02
A06 F10
如何限制结果以仅显示与 D01 合作过的前 5 位演员以及这些演员出演的所有电影?感谢您抽出时间,如果能得到任何帮助,我将不胜感激!
我认为这样的方法可行
select
*
from
actors a
inner join film_actor_director_genre fadg on
on fadg.aid = a.aid
inner join film f on
f.fid = fadg.fid
-- This should return all actors that have been in
-- at lead 5 movies with a director. Note the count
-- ordering and limit
inner join (
select top 5
a.aid
from
film_actor_director_genre
group by
a.aid,
d.did
having
count(*) >= 5
order by
count(*)
) t1 on
t1.aid = a.aid
有点棘手,无法测试查询。应该可以!
这是访问?
假设他的查询是正确的,你可以用更简单的东西替换限制(和有):
select top 5 aid, count(*) as qty from film_actor_director_genre group by aid, did order by 2 desc
如果访问无法处理该 order by 子句,您可以将其切换回
select top 5 aid, count(*) as qty from film_actor_director_genre group by aid, did order by count(*) desc
我正在尝试从电影数据库中获取与特定导演合作过的演员列表,以及这些演员出演过的电影列表。
SELECT DISTINCT A_First_Name, A_Last_Name, Film_Title
FROM
(SELECT *
FROM Actor, Film, Film_Actor_Director_Genre
WHERE Actor.AID=Film_Actor_Director_Genre.AID
AND Film.FID=Film_Actor_Director_Genre.FID)
WHERE A_First_Name IN
(SELECT A_First_Name
FROM
(SELECT *
FROM Actor, Director,
Film_Actor_Director_Genre
WHERE
Actor.AID=Film_Actor_Director_Genre.AID
AND
Director.DID=Film_Actor_Director_Genre.DID)
WHERE D_First_Name=[Director’s first name?]
OR D_Last_Name=[Director’s last name?]);
假设我想知道导演D01合作过的演员名单,示例结果是这样的:
Actor Film
----- ----
A01 F01
A01 F02
A01 F03
A01 F04
A02 F01
A02 F02
A02 F05
A02 F06
A03 F01
A04 F01
A04 F02
A04 F09
A05 F01
A05 F02
A06 F10
A07 F01
A07 F02
A07 F04
etc. etc.
然而,由于与 D01 合作过的演员名单可能非常长,我只想展示前 5 名演员(即与 D01 合作过最多电影的演员),以及与演员合作过的所有电影一直在。所以我想只限演员不限片。期望的结果是:
Actor Film
----- ----
A01 F01
A01 F02
A01 F03
A01 F04
A02 F01
A02 F02
A02 F05
A02 F06
A04 F01
A04 F02
A04 F09
A05 F01
A05 F02
A07 F01
A07 F02
A07 F04
我尝试在各种子查询中实现 SELECT DISTINCT TOP 5
语法,但它们返回了 5 个结果的奇怪组合,例如
Actor Film
----- ----
A01 F01
A01 F02
A02 F01
A02 F02
A06 F10
如何限制结果以仅显示与 D01 合作过的前 5 位演员以及这些演员出演的所有电影?感谢您抽出时间,如果能得到任何帮助,我将不胜感激!
我认为这样的方法可行
select
*
from
actors a
inner join film_actor_director_genre fadg on
on fadg.aid = a.aid
inner join film f on
f.fid = fadg.fid
-- This should return all actors that have been in
-- at lead 5 movies with a director. Note the count
-- ordering and limit
inner join (
select top 5
a.aid
from
film_actor_director_genre
group by
a.aid,
d.did
having
count(*) >= 5
order by
count(*)
) t1 on
t1.aid = a.aid
有点棘手,无法测试查询。应该可以!
这是访问?
假设他的查询是正确的,你可以用更简单的东西替换限制(和有):
select top 5 aid, count(*) as qty from film_actor_director_genre group by aid, did order by 2 desc
如果访问无法处理该 order by 子句,您可以将其切换回
select top 5 aid, count(*) as qty from film_actor_director_genre group by aid, did order by count(*) desc