【发布时间】: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