为什么我的 SQL 脚本 select 输出太多 (CS50 pset7 sql.13)?
Why does my SQL script select too many outputs (CS50 pset7 sql.13)?
我目前正在处理 cs50 中的 pset7,我认为我已经 select 指定了正确数量的值,但我的脚本输出了 349 行而不是答案键的 176 行。
"在 13.sql 中,编写一个 SQL 查询以列出主演过凯文·贝肯 (Kevin Bacon) 主演的电影的所有人的姓名。
您的查询应该输出一个 table,每个人的名字只有一列。
数据库中可能有多个名为 Kevin Bacon 的人。一定要 select 1958 年出生的 Kevin Bacon。
Kevin Bacon 本人不应包含在结果列表中。"
CS50 pset7 sql.13
sqlite> .schema
CREATE TABLE movies (
id INTEGER,
title TEXT NOT NULL,
year NUMERIC,
PRIMARY KEY(id)
);
CREATE TABLE stars (
movie_id INTEGER NOT NULL,
person_id INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id),
FOREIGN KEY(person_id) REFERENCES people(id)
);
CREATE TABLE directors (
movie_id INTEGER NOT NULL,
person_id INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id),
FOREIGN KEY(person_id) REFERENCES people(id)
);
CREATE TABLE ratings (
movie_id INTEGER NOT NULL,
rating REAL NOT NULL,
votes INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id)
);
CREATE TABLE people (
id INTEGER,
name TEXT NOT NULL,
birth NUMERIC,
PRIMARY KEY(id)
);
我的脚本
SELECT DISTINCT name
FROM
people
INNER JOIN stars ON people.id = stars.person_id
INNER JOIN movies ON movies.id = stars.movie_id
WHERE movies.title IN (
SELECT
title
From
movies
INNER JOIN stars ON movies.id = stars.movie_id
INNER JOIN people ON stars.person_id= people.id
WHERE
people.name = "Kevin Bacon"
AND
people.birth = "1958"
)
EXCEPT SELECT name FROM people WHERE people.name = "Kevin Bacon"
这个脚本中是否存在一些逻辑错误?我的逻辑是:
- Select 凯文·培根主演的所有电影(嵌套 SELECT)
- Select 出现在这些凯文·培根电影中的明星(主要 SELECT)的名字,凯文·培根本人除外。
像这样的东西可以在 postgres 中使用。可能需要适应你的数据库。
select name
from (
with kb_movies as
(select distinct movies.id as kb_movie_id
from movies
join stars
on stars.movie_id = movies.id
join people
on people.id = stars.people_id
where people.name = 'Kevin Bacon'
and people.birth = '1958' --or 1958
)
select distinct people.name
from people
join stars
on stars.people_id = people.id
join movies
on movies.id = stars.movie_id
join kb_movies
on kb_movie_id = movies.id
)z
where name <> 'Kevin Bacon'
我目前正在处理 cs50 中的 pset7,我认为我已经 select 指定了正确数量的值,但我的脚本输出了 349 行而不是答案键的 176 行。
"在 13.sql 中,编写一个 SQL 查询以列出主演过凯文·贝肯 (Kevin Bacon) 主演的电影的所有人的姓名。 您的查询应该输出一个 table,每个人的名字只有一列。 数据库中可能有多个名为 Kevin Bacon 的人。一定要 select 1958 年出生的 Kevin Bacon。 Kevin Bacon 本人不应包含在结果列表中。"
CS50 pset7 sql.13
sqlite> .schema
CREATE TABLE movies (
id INTEGER,
title TEXT NOT NULL,
year NUMERIC,
PRIMARY KEY(id)
);
CREATE TABLE stars (
movie_id INTEGER NOT NULL,
person_id INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id),
FOREIGN KEY(person_id) REFERENCES people(id)
);
CREATE TABLE directors (
movie_id INTEGER NOT NULL,
person_id INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id),
FOREIGN KEY(person_id) REFERENCES people(id)
);
CREATE TABLE ratings (
movie_id INTEGER NOT NULL,
rating REAL NOT NULL,
votes INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id)
);
CREATE TABLE people (
id INTEGER,
name TEXT NOT NULL,
birth NUMERIC,
PRIMARY KEY(id)
);
我的脚本
SELECT DISTINCT name
FROM
people
INNER JOIN stars ON people.id = stars.person_id
INNER JOIN movies ON movies.id = stars.movie_id
WHERE movies.title IN (
SELECT
title
From
movies
INNER JOIN stars ON movies.id = stars.movie_id
INNER JOIN people ON stars.person_id= people.id
WHERE
people.name = "Kevin Bacon"
AND
people.birth = "1958"
)
EXCEPT SELECT name FROM people WHERE people.name = "Kevin Bacon"
这个脚本中是否存在一些逻辑错误?我的逻辑是:
- Select 凯文·培根主演的所有电影(嵌套 SELECT)
- Select 出现在这些凯文·培根电影中的明星(主要 SELECT)的名字,凯文·培根本人除外。
像这样的东西可以在 postgres 中使用。可能需要适应你的数据库。
select name
from (
with kb_movies as
(select distinct movies.id as kb_movie_id
from movies
join stars
on stars.movie_id = movies.id
join people
on people.id = stars.people_id
where people.name = 'Kevin Bacon'
and people.birth = '1958' --or 1958
)
select distinct people.name
from people
join stars
on stars.people_id = people.id
join movies
on movies.id = stars.movie_id
join kb_movies
on kb_movie_id = movies.id
)z
where name <> 'Kevin Bacon'