【问题标题】:The Shahrukh question on imbd dataset, certainly is tricky关于 imdb 数据集的 Shahrukh 问题,当然很棘手
【发布时间】:2021-01-25 23:02:29
【问题描述】:

**

完整的问题:演员的沙鲁克数是 演员和沙鲁克汗之间的最短路径 “共同作用”图。也就是说,Shahrukh Khan 的 Shahrukh 编号为 0;全部 与沙鲁克在同一部电影中表演的演员的沙鲁克排名第一; 与Shahrukh的某些演员在同一部电影中演出的所有演员 1 号有 Shahrukh 2 号等。返回所有拥有 Shahrukh 的演员 数字是 2。

**

我的 SQL 查询:

#loading SQL module
%load_ext sql

#connect to the database
%sql sqlite:///Db-IMDB.db

%%time
%%sql

UPDATE Movie SET year = REPLACE(year, "I", "");
UPDATE Movie SET year = REPLACE(year, "V", "");
UPDATE Movie SET year = REPLACE(year, "X ", "");
UPDATE Movie SET title = LTRIM(title);
UPDATE Movie SET year = RTRIM(LTRIM(year));
UPDATE Movie SET rating = RTRIM(LTRIM(rating));
UPDATE Movie SET num_votes = RTRIM(LTRIM(num_votes));

UPDATE M_Producer SET pid = RTRIM(LTRIM(pid));
UPDATE M_Producer SET mid = RTRIM(LTRIM(mid));

UPDATE M_Director SET pid = RTRIM(LTRIM(pid));
UPDATE M_Director SET mid = RTRIM(LTRIM(mid));

UPDATE M_Cast SET pid = RTRIM(LTRIM(pid));
UPDATE M_Cast SET mid = RTRIM(LTRIM(mid));

UPDATE M_Genre SET gid = RTRIM(LTRIM(gid));
UPDATE M_Genre SET mid = RTRIM(LTRIM(mid));

UPDATE Genre SET gid = RTRIM(LTRIM(gid));
UPDATE Genre SET name = RTRIM(LTRIM(name));

UPDATE Person SET name = RTRIM(LTRIM(name));
UPDATE Person SET pid = RTRIM(LTRIM(pid));
UPDATE Person SET gender = RTRIM(LTRIM(gender));

%%time
%%sql

select distinct PID, 
Name 
from Person natural 
join M_Cast
where Name != ‘Shah Rukh Khan’ and MID in 
(select MID from M_Cast 
 where PID in 
 (select PID 
  from Person natural 
  join M_Cast
  where Name != ‘Shah Rukh Khan’ and MID in 
  (select MID
   from Person natural 
   join M_Cast
   where Name != ‘Shah Rukh Khan’)))and PID not in 
(select PID
 from Person natural 
 join M_Cast where Name != ‘Shah Rukh Khan’ and MID in 
 (select MID
  from Person natural 
  join M_Cast where Name = ‘Shah Rukh Khan’))
limit 7;

输出:- 我得到一个错误。

* sqlite:///Db-IMDB-Assignment1.db
(sqlite3.OperationalError) near "Rukh": syntax error
[SQL: select distinct PID, Name 
from Person natural 
join M_Cast
where Name != ‘Shah Rukh Khan’ and MID in 
(select MID from M_Cast 
 where PID in 
 (select PID 
  from Person natural 
  join M_Cast
  where Name != ‘Shah Rukh Khan’ and MID in 
  (select MID
   from Person natural 
   join M_Cast
   where Name != ‘Shah Rukh Khan’)))and PID not in 
(select PID
 from Person natural 
 join M_Cast where Name != ‘Shah Rukh Khan’ and MID in 
 (select MID
  from Person natural 
  join M_Cast where Name = ‘Shah Rukh Khan’))
limit 7;]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
Wall time: 51.7 ms

架构:

find the schema to the problem statement here

数据库:The DB link can be obtained here

需要帮助。提前致谢。

从反引号到字符串上的双引号编辑后::

%%time
%%sql

select distinct PID, 
Name 
from Person natural 
join M_Cast
where Name != "Shah Rukh Khan" and MID in 
(select MID from M_Cast 
 where PID in 
 (select PID 
  from Person natural 
  join M_Cast
  where Name != "Shah Rukh Khan" and MID in 
  (select MID
   from Person natural 
   join M_Cast
   where Name != "Shah Rukh Khan")))and PID not in 
(select PID
 from Person natural 
 join M_Castwhere Name != "Shah Rukh Khan" and MID in 
 (select MID
  from Person natural 
  join M_Castwhere Name = "Shah Rukh Khan"))
limit 7;

仍然面临错误

* sqlite:///Db-IMDB-Assignment1.db
(sqlite3.OperationalError) near "!=": syntax error
[SQL: select distinct PID, Name 
from Person natural 
join M_Cast
where Name != "Shah Rukh Khan" and MID in 
(select MID from M_Cast 
 where PID in 
 (select PID 
  from Person natural 
  join M_Cast
  where Name != "Shah Rukh Khan" and MID in 
  (select MID
   from Person natural 
   join M_Cast
   where Name != "Shah Rukh Khan")))and PID not in 
(select PID
 from Person natural 
 join M_Castwhere Name != "Shah Rukh Khan" and MID in 
 (select MID
  from Person natural 
  join M_Castwhere Name != "Shah Rukh Khan"))
limit 7;]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
Wall time: 6.98 ms

刚刚发现解决方案的正确的行数是 25698,而上述解决方案的输出是 48 行。需要你的帮助。谢谢

【问题讨论】:

  • PC:我一直在我的 python 笔记本上执行这个。试过“row_number() over partition”,报错,发现MySQL不使用。
  • @AkshayKumar row_number 和其他窗口函数需要 mysql 8.0 或 mariadb 10.2。

标签: mysql


【解决方案1】:

我只是在描述逻辑,所以下面的代码不会在您的架构上运行。

您可以获得与 Shahrukh Khan (SK=0) 合作过的所有演员

CREATE VIEW sk1 AS (                            /* SK=1 actors are... */
SELECT a1.id FROM actors AS a1                  /* ...those actors... */ 
  JOIN cast AS c1 ON (c1.actor_id = a1.id)      /* ...who casted... */
  JOIN cast AS sk ON (sk.film_id = c1.film_id)  /* ...in the same film cast... */
  JOIN actor AS a0 ON (sk.actor_id = a0.id AND a0.name = 'Shahrukh Khan')     /* as the actor, whose name is 'Shahrukh Khan' */
  WHERE a1.id != a0.id                          /* but are not him */
);

现在 SK2 演员在其他演员中

CREATE VIEW oc AS (                             /* Other actors are... */
    SELECT c1.actor_id AS id FROM cast AS c1    /* ...those who casted... */ 
    LEFT JOIN sk1 ON (sk1.id = c1.actor_id)     /* ...related to actors in SK1... */
    LEFT JOIN actor AS a0 ON (c1.actor_id = a0.id AND a0.name = 'Shahrukh Khan')
                                                /* ...and the actors who are SK... */
    WHERE sk1.id IS NULL AND a0.id IS NULL      /* ...by not being there. */
) AS otherActors;

在 otherActors 中的演员,并在 SK1 中与演员一起演员,其 SK 为 2:

SELECT COUNT(*) FROM (
SELECT otherActors.id FROM otherActors AS oc
    JOIN cast AS c1 ON (oc.id = c1.actor_id)
    JOIN cast AS c2 ON (c1.film_id = c2.film_id)
    JOIN sk1 ON (c2.actor_id = sk1.id)
) AS sk2;

你会注意到上面有一些无用的 JOIN(我从不需要来自 actor a1 的数据,因为我得到的数据 a1.id 与 c1.actor_id 的定义相同)。不过,如果您需要名称,它可能会派上用场。或者,当(如果)您需要超出其 ID 的演员信息时,您可以进行进一步的 JOIN。我也从不需要电影中的数据。但是,使用 actor 会强制每个 actor 只出现在场景中一次;如果我直接使用演员表,那么我很可能会找到重复的演员 ID,从而迫使我添加 DISTINCT 子句。

最后,这是一个非递归、非可编程的实现。 SK=3 将迫使我添加另一种观点和进一步的复杂性。 MySQL 8.0+ 具有递归 CTE 支持,这完全改变了游戏规则(你可以适应 this answer,但要注意循环;你想添加一个显式检查,以便“下一个”集合总是在没有 SK 编号的演员)。

【讨论】:

    【解决方案2】:

    您正在使用反引号将字符串文字括起来; mysql 需要单引号或双引号。在 mysql 中,反引号用于引用可能与保留字冲突的标识符,而不是字符串文字。

    所以替换每个

    ‘Shah Rukh Khan’
    

    与

    "Shah Rukh Khan"
    

    【讨论】:

    • 首先谢谢你。我已经尝试过,但仍然出现错误,我已经更新了问题信息。上面的区域。需要你的帮助。
    • 好的,我在代码中分离了 "where" 子句后得到了一定的输出,我没有得到问题陈述的正确答案。您能帮我找到上述问题的正确解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    相关资源
    最近更新 更多