再次新的查询。我试图从一个小时解决这个问题。请帮忙

时间:2010-05-06 09:27:21

标签: sql mysql sql-server

查询:列出所有“朱莉安德鲁斯”电影的电影片名和主角。

有三个表

  

电影(身份证,头衔,年份,得分,投票,导演)

     

演员(身份证,姓名)

     

施放(movieid,actorid,ord)

select movie.title,actor.name as cont
from movie
join casting on (movie.id=casting.movieid)
join actor on (casting.actorid=actor.id)
where actor.name='Julie andrews'
实际上我无法找到如何找到主角。

3 个答案:

答案 0 :(得分:0)

首先,您不应将id用于关键列,请使用movieidactorid。这使得连接更容易:

select movie.title, lead.name as cont
  from actor
  join casting             using (actorid)
  join movie               using (movieid)
  join casting as leadrole using (movieid)
  join actor   as lead     on (leadrole.actorid = lead.actorid)
 where actor.name = 'Julie andrews'
   and leadrole.ord = 1

这假设主角的ord列为1.它也是完全未经测试的,因此您可能需要对其进行调试。

答案 1 :(得分:0)

假设主角是ord = 1你可以尝试一下(我没有测试它)

编辑:

select 
      M.title, 
      (select 
            A1.name 
       from 
            casting CI
       join
            actors A1
       on
            A1.id = CI.actorid      
       where 
            CI.movieid = M.id 
       and 
            CI.ord = 1) as LeadingActor 
from 
      movie M
join 
      casting C
on 
      M.id = C.movieid
join 
      actor A 
on 
      C.actorid = A.id
where 
      A.name='Julie andrews'

答案 2 :(得分:0)

您几乎肯定会需要一个子查询来实现这种或那种方式。

select movie.title, actor.name
from movie
inner join casting 
    on movie.id = casting.movieid
    and casting.ord = 1 --Assuming this is the cast order flag
inner join actor
    on casting.actorid = actor.id
where movie.id in (
    select movie.id
    from movie 
    inner join casting 
        on movie.id = casting.movieid
        and casting.actorid = {your actorID in this case Julie Andrew's id
 )

免责声明:我没有对此进行测试,因为您没有提供一种简单的方法。

如果你真的想通过名字(可能导致误报),那么你可以在in子句子查询中添加额外的连接。