我在这个问题上苦苦挣扎。也许有人可以给我一个暗示?
我的设置包含3个表格(演员,电影和关系)。我想做的是:
查找名字以A开头的所有演员,这些演员在Person1旁边的任何电影中播放。
这就是我有多远:
SELECT Name
FROM Actors a, Movies m, Relations r
WHERE a.Name='A%'
AND Person1.keyPerson = m.KeyPerson
AND m.KeyMovies ....
关于如何继续的例子很不错!
UPADATE - 表格如下所示:
演员(actorsID,名字) 电影(moviesID,标题) 关系(actorsID,moviesID)
答案 0 :(得分:1)
有几种方法可以做到这一点 - 您可以通过IN
语句,拉动电影Person1
以及检查A%
人的电影来实现一种方法。
Select Distinct A.Name
From Actors A
Join Relations R On R.ActorId = A.ActorId
Where A.Name Like 'A%'
And R.MovieId In
(
Select R2.MovieId
From Actors A2
Join Relations R2 On R2.ActorId = A2.ActorId
Where A2.Name = 'Person1'
)
另一种方法是通过EXISTS
声明,查找Person1
所在的A%
所有的电影:
Select A.Name
From Actors A
Join Relations R On R.ActorId = A.ActorId
Where A.Name Like 'A%'
And Exists
(
Select *
From Movies M2
Join Relations R2 On R2.MovieId = M2.MovieId
Join Actors A2 On R2.ActorId = R2.ActorId
Where A2.Name = 'Person1'
And M2.MovieId = R.MovieId
)
答案 1 :(得分:0)
没有子查询的解决方案:
SELECT DISTINCT a.Name
FROM Actors a
INNER JOIN Relations r
ON r.actorsID = a.actorsID
INNER JOIN Relations r2
ON r.moviesID = r2.moviesID
INNER JOIN Actors a2
ON a2.actorsID = r2.actorsID
AND a2.Name = 'Person1'
WHERE a.Name LIKE 'A%'