我正在使用基本SQL进行一些练习,但遇到以下问题:
让我们考虑以下有关演员和电影的关系图式:
让我们假设,一个电影中可以有不止一个演员,而在一部电影中,同一个演员也可以,而在一部给定的电影中,每个参与的演员仅扮演一个角色。电影制片人必须由其姓氏唯一地标识,每部电影由一位制片人导演。
我必须写的查询是:
在电影制片人昆汀·塔伦蒂诺(Quentin Tarantino)执导的最多一部电影中一起表演的演员。
如何将“最多一部电影”翻译成SQL语言?
到目前为止我写的是:
SELECT DISTINCT A1.ActorCode, A2.ActorCode
FROM Actor A1 A2, Interpretation I1 I2
WHERE I1.Film=I2.Film and I1.Actor <> I2.Actor and A1.ActorCode= I1.Actor and A2.ActorCode=I2.ActorCode and exists unique (
Select *
From Film F
Where I1.Film=F.FilmCode and F.Filmmaker=’Tarantino’
)
但这不是重点
SELECT
I.Actor, I2.Actor
FROM Interpretation I
INNER JOIN FIlm F
ON F.Filmmaker=’Tarantino’
and F.FilmCode = I.Film
INNER JOIN Interpretation I2
ON I.Film = I2.Film
and I2.Actor <> I.Actor
and I2.Actor not in (SELECT Actor
FROM Interpretation I3
INNER JOIN FIlm F1
ON F1.Filmmaker=’Tarantino’
and F1.FilmCode = I3.Film
and F1.FilmCode <> I.Film #All other tarantino films
WHERE I3.Actor = I.Actor)