我的问题是:11题和13题,都是有两个变量,为什么第11题不需要用子查询,而第13题需要?
-- # 11. List the films together with the leading star for all 1962 films.
SELECT movie.title, actor.name
FROM movie
JOIN casting
ON casting.movieid = movie.id
JOIN actor
ON actor.id = casting.actorid
WHERE movie.yr = 1962
AND casting.ord = 1;
-- # 13. List the film title and the leading actor for all of 'Julie
-- # Andrews' films.
select title, name
from movie
join casting
on (movieid=movie.id and ord =1)
join actor
on (actorid=actor.id)
where movie.id in
(select movieid from casting
where actorid in
(select id from actor
where name = 'Julie Andrews'))
子查询和关联查询本来就是可以相互转换的,考虑到sql的优化,所以在不同场景使用不同类型的查询。
分析:
13、题目大概意思是列出朱丽叶电影作者的所有电影。
这个时候单纯用关联查询就实现不了。因为朱丽叶电影的作者是未知的,所以需要通过子查询得到作者信息(根据电影名得到作者,然后根据作者信息能查询到所有的电影信息),最后将子查询的结果作为条件进行过滤。
注意:如果要求列出朱丽叶电影的相关信息,查询就跟11题基本一致。
如有帮助,望点击我回答右上角【采纳】按钮支持一下。