错误代码:1054“in/all/any子查询”中的未知列“movie\u id”

vu8f3i0k  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(587)
SELECT DISTINCT actor_id 
    FROM 
      (SELECT DISTINCT actor_id
      FROM cast 
      WHERE NOT movie_id in 
            (SELECT movie_id 
            FROM cast 
            INNER JOIN actors 
            ON actors.ID = cast.actor_id 
            WHERE full_name = 'Kevin Bacon')) as A
     WHERE movie_id in 
     (SELECT movie_id  
     FROM cast 
     WHERE actor_id in
        (SELECT DISTINCT actor_id 
        FROM cast 
        WHERE movie_id in 
            (SELECT movie_id  
            FROM cast 
            INNER JOIN actors 
            ON actors.ID = cast.actor_id 
            WHERE full_name = 'Kevin Bacon')))
   AND actor_id <> (SELECT id from actors
   where full_name = "Kevin Bacon")
;

我一直在“in/all/any subquery”中遇到未知列“movie_id”的错误;这我不明白,因为这段代码的块采取单独的工作刚刚好。
我错过了什么?
太棒了!

2wnc66cl

2wnc66cl1#

我在这个简化的查询示例中看到了错误:

SELECT DISTINCT actor_id
FROM ( SELECT DISTINCT actor_id FROM ...) as A
WHERE movie_id in (...);

在where子句中,您引用的是“a”表中的“movie\u id”,但在内部查询(select distinct actor\u id from…)中,此列未被选中。
而且,有这么多过时的查询,我相信这可以简化,如果你给一个例子“用词”你想得到什么。
对目标的响应
我没有为你的目标找到一个简单的答案,但我会这样做:
首先,我会根据演员们的电影来创造一个他们之间的关系。。。

CREATE VIEW vw_relations AS (
    SELECT
        c1.actor_id AS actor1_id, a1.full_name AS actor1_fullname,
        c1.movie_id, m.title AS movie_title,
        c2.actor_id AS actor2_id, a2.full_name AS actor2_fullname
    FROM
        cast AS c1
    INNER JOIN
        cast AS c2 ON c2.movie_id = c1.movie_id AND c2.actor_id != c1.actor_id
    INNER JOIN
        movies AS m ON m.id = c1.movie_id
    INNER JOIN
        actors AS a1 ON a1.id = c1.actor_id
    INNER JOIN
        actors AS a2 ON a2.id = c2.actor_id
);

现在,如果actor name1与actor name2参与了同一部电影,则在上一个视图中会有具有下一个值的行:

(id_name1, name1, movie_id, movie_title, id_name2, name2)
(id_name2, name2, movie_id, movie_title, id_name1, name1)

换句话说,关系将出现两次,但这将简化下一个查询。。。
现在,根据你的定义,1º 演员“凯文·培根”(与他共事的演员)的亲密度可以如下获得:

CREATE VIEW vw_1_degree_to_kb AS (
    SELECT
        actor1_id, actor1_fullname
    FROM
        vw_relations
    WHERE
        actor2_fullname = "Kevin Bacon"
);

现在,对于2º 演员“凯文·培根”(和他一起工作的演员)的亲密度我会这样做(也会保存在一个视图中):

CREATE VIEW vw_2_degree_to_kb AS (
    SELECT
        actor1_id, actor1_fullname
    FROM
        vw_relations
    WHERE
        actor2_id IN (SELECT actor1_id FROM vw_1_degree_to_kb)
    AND
        actor1_id NOT IN (SELECT actor1_id FROM vw_1_degree_to_kb)
    AND
        actor1_fullname != "Kevin Bacon"
);

换句话说,此视图包含与1一起工作的参与者º 《凯文·培根》中演员的亲密度,但那还不属于那一套。
更重要的是,3º 接近程度如下:

CREATE VIEW vw_3_degree_to_kb AS (
    SELECT
        actor1_id, actor1_fullname
    FROM
        vw_relations
    WHERE
        actor2_id IN (SELECT actor1_id FROM vw_2_degree_to_kb)
    AND
        actor1_id NOT IN (SELECT actor1_id FROM vw_1_degree_to_kb)
    AND
        actor1_id NOT IN (SELECT actor1_id FROM vw_2_degree_to_kb)
    AND
        actor1_fullname != "Kevin Bacon"
);

相关问题