Select语句返回不正确的结果

时间:2013-11-13 07:12:59

标签: sql select

我一直试图在this教程中找出问题编号13 的正确SQL。没有其他问题与我有关,只有#13。

基本上,问题是要找出'朱莉安德鲁斯'所播放的所有电影,从这个结果中,选择扮演这些电影中主要演员的演员。听起来很简单,但我尝试的一切都失败了。以下内容:

select title, name from movie
  join casting on movie.id=movieid
  join actor on actorid=actor.id
  where (name = 'Julie Andrews' ) 
  and ord=1

选择她所在的电影,她是主角。我需要的是她所在的电影主角,而不是她所扮演的电影。

有没有人有任何建议?

2 个答案:

答案 0 :(得分:0)

select movie.title, actor.name
from movie, actor, casting, (SELECT movieid from casting join actor 
on actor.id=casting.actorid where actor.name = 'Julie Andrews') JAM
where movie.id = JAM.movieid and actor.id = casting.actorid
and casting.movieid=JAM.movieid and casting.ord=1
group by movie.title

答案 1 :(得分:0)

试试这个:

select m.title, a.name
from movie m
join casting c on m.id = c.movieid
   and ord = 1
join actor a on a.id = c.actorid
where exists (select *
   from casting c2
   join actor a2 on c2.actorid = a2.id
      and a2.name = 'Julie Andrews'
   where c2.movieid = m.id
)