如何列出三向连接表查询中的第一个值?

时间:2009-01-11 23:31:51

标签: mysql sql mysql-5.0

好吧,我很难解释事情,所以我先给你报价和链接:

Problem 4b(靠近底部):

  

4b中。列出所有'朱莉安德鲁斯'电影的电影片名和主角。

     
    

电影(身份证,头衔,年份,分数,票数,导演)
    演员(身份证,姓名)
    施法(movieid,actorid,ord)
    (注意:movie.id = casting.movi​​eid,actor.id = casting.actorid)

  

我的回答(不起作用):
    SELECT title, name
      FROM casting JOIN movie
              ON casting.movieid = movie.id
      JOIN actor
              ON casting.actorid = actor.id
     WHERE name = 'Julie Andrews'
       AND ord = 1

这里的问题是,它希望电影的主要演员名单与'朱莉安德鲁斯'作为一个演员(谁不一定是主演),但我所做的所有我的回答是把电影放在哪里她是领导(ord = 1)。

如何在没有“Julie Andrews”的情况下指定主要演员名单?我怀疑我必须对GROUP BY做些什么,但我现在无法弄明白...

编辑:我是否需要使用嵌套的SELECT?

9 个答案:

答案 0 :(得分:6)

使用子查询有很好的方法可以做到这一点,但是在教程中你似乎只使用了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)加入,以获得这些电影的主要角色列表: / p>

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'关键字是可选的。我已将其插入上面以帮助查询更有意义

答案 1 :(得分:4)

你想把电影与casting表中两个可能分开的行相匹配:朱莉安德鲁斯是演员的第一行,第二行可能是也可能不是朱莉安德鲁斯,但是哪个是主角这部电影。

  

朱莉< --->施放于< --->电影< --->主演< --->主演

所以你需要两次加入casting表。

SELECT m.title, lead.name
FROM actor AS julie
 JOIN casting AS c1 ON (julie.id = c1.actorid)
 JOIN movie AS m ON (c1.movieid = m.id)
 JOIN casting AS c2 ON (m.id = c2.movieid)
 JOIN actor AS lead ON (c2.actorid = lead.id)
WHERE julie.name = 'Julie Andrews'
 AND c2.ord = 1;

请记住,“表别名”引用了可能不同的行,即使它们是同一个表的别名。

答案 2 :(得分:3)

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

答案 3 :(得分:1)

顺便说一下,这是网站上发布的答案(我刚刚发现了它):

SELECT title, name
  FROM movie, casting, actor
  WHERE movieid=movie.id
    AND actorid=actor.id
    AND ord=1
    AND movieid IN
    (SELECT movieid FROM casting, actor
     WHERE actorid=actor.id
     AND name='Julie Andrews')

去图。 :P

答案 4 :(得分:0)

select title, name  from movie join casting on movie.id=movieid
join actor on actor.id=actorid 
where ord=1 and movieid in (select movieid from actor join casting 
                                            on actor.id=actorid 
         where name='julie andrews' and (ord=1 or ord<>1))group by title, name

答案 5 :(得分:0)

@Bill Karwin的回答对方向有好处。 然而,结果证明一些电影的标题已出现不止一次。 所以我们需要在SELECT行中的title之前添加一个DISTINCT函数。

SELECT DISTINCT m.title, lead.name

如果你想要更快的响应,你也可以用这种方式编写代码。

SELECT DISTINCT m.title, lead.name
 FROM actor AS julie
  JOIN casting AS c1 ON (julie.id = c1.actorid AND julie.name = 'Julie Andrews')
  JOIN movie AS m ON (c1.movieid = m.id)
  JOIN casting AS c2 ON (m.id = c2.movieid)
  JOIN actor AS lead ON (c2.actorid = lead.id)
 WHERE  c2.ord = 1;

答案 6 :(得分:0)

I ve done simply like this

select distinct title,name from 
(select movie.* from movie
inner join casting on (movie.id = casting.movieid)
inner join actor on (casting.actorid = actor.id)
where actor.name = 'Julie Andrews' ) as moviesFromJulie

inner join casting on (moviesFromJulie.id = casting.movieid)
inner join actor on (casting.actorid = actor.id)
where ord = 1

回归所有电影julie andrews演奏,然后为那些电影用序数= 1来回溯所有电影和演员,并且独特的结果

答案 7 :(得分:0)

分享一个简单的答案 -

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

答案 8 :(得分:0)

SELECT title , name
FROM movie JOIN casting ON (casting.movieid=movie.id) JOIN actor ON (actor.id=actorid)
WHERE (movieid IN (SELECT movieid
FROM casting JOIN actor ON (actor.id=actorid)
WHERE name =  'Julie Andrews')) AND (ord=1)