SQLzoo |加入操作

时间:2013-08-17 11:15:28

标签: sql join

我正在尝试解决SQLzoo中的JOIN教程问题13(http://sqlzoo.net/wiki/More_JOIN_operations

这个问题要求列出所有电影'朱莉安德鲁斯'的电影片名和主要演员。

我已经制作了以下脚本,但某处出错了。

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

7 个答案:

答案 0 :(得分:1)

正确的脚本是:

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

我注意到如果我在WHERE语句中使用列m.title作为代理,则脚本需要更多时间来运行,而SQLzoo服务器会将其停止。相反,使用m.id列可以加快加载速度。也许是一个好习惯总是喜欢在可能的情况下检查数字而不是字母。

我的初始脚本的另一个问题是设置EQUAL运算符而不是IN。子查询返回多于1行,因此必须使用IN运算符。

答案 1 :(得分: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')

答案 2 :(得分:0)

我认为这是你获得它的正确方法。

SELECT m.title, a.name FROM
  movie m JOIN casting c ON c.id = m.movieid
        JOIN actor a ON a.id=m.actorid
    WHERE m.ord=1
    AND m.title = ALL
      (SELECT m1.title FROM 
         movie m1 JOIN casting c1 ON c1.id=m1.movieid
               JOIN actor a1 ON a1.id=m1.actorid
           WHERE a1.name='Julie Andrews');

如果您必须使用列名显示表,那么我将修改此查询

答案 3 :(得分:0)

select title, name from movie
join casting on (casting.movieid =movie.id)
join actor on (actor.id = casting.actorid)

where ord =1
and movieid in (select movie.id from movie
join casting on (casting.movieid =movie.id)
join actor on (actor.id = casting.actorid)

where name = 'Julie Andrews')

答案 4 :(得分:0)

简便解决方案:

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

答案 5 :(得分:0)

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

从子查询开始:选择所有带有“朱莉·安德鲁斯”的电影的“电影”,并将其用作WHERE中的过滤器

现在以特定条件联接所有3个表,即您仅选择'ord'= 1的'actorid'('ord'= 1意味着actor名称位于actor的顶部)。这只会选择包含主要演员的演员ID的行/记录。最后,只需从“演员”表中检索这些主要演员的名字即可。

答案 6 :(得分:-1)

TypeError: Cannot read property 'getLoginStatus' of undefined
 at Scope.$scope.facebookSignIn (controllers.js:547)
 at $parseFunctionCall (ionic.bundle.js:21172)
 at ionic.bundle.js:53674
 at Scope.$eval (ionic.bundle.js:23228)
 at Scope.$apply (ionic.bundle.js:23327)
 at HTMLAnchorElement.<anonymous> (ionic.bundle.js:53673)
 at HTMLAnchorElement.eventHandler (ionic.bundle.js:11841)
 at triggerMouseEvent (ionic.bundle.js:2865)
 at tapClick (ionic.bundle.js:2854)
 at HTMLDocument.tapTouchEnd (ionic.bundle.js:2977)