CS50 Pset 7 13.sql,我无法解决,嵌套了sqlite3数据库

CS50 Pset 7 13.sql,我无法解决,嵌套了sqlite3数据库

问题描述:

数据库movie.db

DataBase movies.db

tables

导演(movie_id,person_id)

directors (movie_id, person_id)

电影(id,职称,年份)

movies (id, title, year)

人(身份证,姓名,出生)

people (id, name, birth)

评分(电影ID,评分,投票)

ratings (movie_id, rating, votes)

星星(电影ID,人物ID)

stars (movie_id, person_id)

您可以下载 数据库

you can download the database.

问题

Question

我是编程新手,所以我决定首先从CS50哈佛课程开始,这是问题所在,并且是测试解决方案

I'm new in programming, so I decided, to begin with, CS50 Harvard course here is the problem and the test solution:

在13.sql中,编写一个SQL查询以列出曾在凯文·培根也曾出演过电影的所有人的名字。
您的查询应该输出一个表格,其中每个人的名字都只有一列。
数据库中可能有多个名为Kevin Bacon的人。确保只选择出生于1958年的凯文·培根。
凯文·培根本人不应该包含在结果列表中。

In 13.sql, write a SQL query to list the names of all people who starred in a movie in which Kevin Bacon also starred. Your query should output a table with a single column for the name of each person. There may be multiple people named Kevin Bacon in the database. Be sure to only select the Kevin Bacon born in 1958. Kevin Bacon himself should not be included in the resulting list.

解决方案提示

执行13.sql会生成具有1列和176行的表。

Executing 13.sql results in a table with 1 column and 176 rows.

在PowerShell或bash
cat 13.sql | sqlite3 films.db

executing code in PowerShell or bash cat 13.sql | sqlite3 movies.db

我的代码:

SELECT COUNT(name)
FROM people 
JOIN stars ON stars.person_id = people.id 
JOIN movies ON movies.id = stars.movie_id 
WHERE people.id IN(
SELECT stars.person_id 
FROM stars 
GROUP BY stars.person_id 
HAVING name = "Kevin Bacon");

问题:

problem:

当我执行代码停止工作时,我无法与Kevin一起选择星星

when I execute code it stop working, I can't choose stars with Kevin

获取自上而下的层次结构中的值,但请确保您在正确的域中进行搜索。获取所需的第一名称,但在取决于person_id的正确域中搜索该名称,而该域依次为movie_id。最后,要调用条件,我们必须重新调用people.id,因为条件取决于people表中的数据。

Get values in top-bottom hierarchy but make sure that you're searching for it in the correct domain. GET the name which is the first thing needed but search for it in the correct domain which is dependent on person_id, in turn to movie_id. Finally to invoke the condition, we've to recall people.id as the condition is dependent on data in the people table. It's essential to do the needed JOINs at each step.

要从结果中删除Kevin Bacon,可以使用EXCEPT关键字,并在其他查询中专门选择他。

To remove Kevin Bacon from results, you can use the EXCEPT keyword and specifically choose him in the other query.

SELECT name FROM people WHERE people.id
IN
( SELECT person_id FROM stars JOIN movies ON movies.id = stars.movie_id 
WHERE movie_id IN
( SELECT movie_id FROM movies JOIN stars ON stars.movie_id = movies.id JOIN people ON 
 people.id = stars.person_id WHERE people.id IN (
SELECT id FROM people WHERE people.name = "Kevin Bacon" AND people.birth = 1958 )))
EXCEPT
SELECT name FROM people WHERE people.name = "Kevin Bacon" AND people.birth = 1958