我有一个任务,选择使用sqlzoo.net"更多加入"指导罗伯特·德尼罗电影,电影片名和年份的所有导演名字。数据库。我设法从数据库中选择了电影标题和所有导演ID。 问题是我不知道如何将ID链接到名称...... 如果你接受我发布的代码,你会看到页面右侧的结果。我需要帮助,所以它显示导演NAMES而不是ID。 请注意,movie.director键与actor.name匹配!需要以某种方式连接它们。
我使用了这段代码:
select distinct movie.director,movie.title,yr
from movie join casting
on movie.id=movieid join actor on actor.id=actorid
where movie.id in(select movie.id
from movie join casting on movie.id=movieid
join actor on actor.id=actorid
where actor.name='Robert De Niro')
order by yr DESC
答案 0 :(得分:0)
假设有一个director
表并且movie.director
字段与director.id
字段匹配,它的外观如何。
select director.name, movie.title, yr
from movie
join casting on movie.id = casting.movieid
join actor on actor.id = movie.actorid
join actor as director on director.id = movie.director
where actor.name='Robert De Niro'
order by yr DESC
答案 1 :(得分:0)
你几乎拥有它,你只需要在外部查询中将联接的条件更改为actor
以使用movie.director
,然后选择actor.name
而不是movie.director
:
select distinct actor.name,movie.title,yr
from movie
join casting on movie.id = movieid
join actor on actor.id = movie.director
where movie.id in(select movie.id
from movie join casting on movie.id=movieid
join actor on actor.id = actorid
where actor.name='Robert De Niro')
order by yr DESC
由于actor表同时具有actor和director,因此使用表别名可以更容易阅读:
select distinct director.name,movie.title,yr
from movie
join casting on movie.id = movieid
join actor AS director on director.id = movie.director
where movie.id in(select movie.id
from movie join casting on movie.id=movieid
join actor on actor.id = actorid
where actor.name='Robert De Niro')
order by yr DESC