使用子查询有很多很好的方法,但在本教程的这一点上,您似乎只使用 JOIN。以下是仅使用 JOIN 的方法:
SELECT
movie.title,
a2.name
FROM
actor AS a1
JOIN casting AS c1 ON (a1.id = c1.actorid)
JOIN movie ON (c1.movieid = movie.id)
JOIN casting AS c2 ON (movie.id = c2.movieid)
JOIN actor AS a2 ON (c2.actorid = a2.id)
WHERE
a1.name = 'Julie Andrews'
AND c2.ord = 1
编辑(更具描述性):
这将为我们提供一个包含 Julie Andrews 出演的所有电影的表格。我将演员表和演员表分别别名为 a1 和 c1,因为现在我们已经找到了电影列表,我们将不得不转向并匹配再次对着铸造台。
SELECT
movie.*
FROM
actor a1
JOIN casting c1 ON (a1.id = c1.actorid)
JOIN movie ON (c1.movieid = movie.id)
WHERE
a1.name = 'Julie Andrews'
现在我们有了她演过的所有电影的列表,我们需要将其与演员表(如 c2)和演员表(如 a2)相结合,以获得这些电影的主角列表:
SELECT
movie.title, -- we'll keep the movie title from our last query
a2.name -- and select the actor's name (from a2, which is defined below)
FROM
actor a1 -- \
JOIN casting AS c1 ON (a1.id = c1.actorid) -- )- no changes here
JOIN movie ON (c1.movieid = movie.id) -- /
JOIN casting AS c2 ON (movie.id = c2.movieid) -- join list of JA movies to the cast
JOIN actor AS a2 ON (c2.actorid = a2.id) -- join cast of JA movies to the actors
WHERE
a1.name = 'Julie Andrews' -- no changes
AND c2.ord = 1 -- only select the star of the JA film
编辑:在别名中,“AS”关键字是可选的。我在上面插入了它以帮助查询更有意义