CS50 Pset 7 13.sql, I can't solve it, nested sqlite3 database

后端 未结 5 874
感动是毒
感动是毒 2020-12-11 14:10

DataBase movies.db

tables

directors (movie_id, person_id)

movies (id, title, year)

people

5条回答
  •  醉梦人生
    2020-12-11 14:35

    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.

    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
    

提交回复
热议问题