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

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

DataBase movies.db

tables

directors (movie_id, person_id)

movies (id, title, year)

people

相关标签:
5条回答
  • 2020-12-11 14:26

    I try the steps in Zara answer:

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

    Result:

    I must have done the wrong steps to get this result

    Expected 176 rows but got one row "Steve Guttenberg"

    0 讨论(0)
  • 2020-12-11 14:26

    I did it without using any Join, just nesting the conditions.

    SELECT people.name FROM people
    WHERE people.id IN 
    (
        SELECT stars.person_id FROM stars
        WHERE stars.movie_id IN 
        (
            SELECT stars.movie_id FROM stars
            WHERE stars.person_id IN
            (
                SELECT people.id FROM people
                WHERE people.name = "Kevin Bacon" AND 
                people.birth = 1958
            )
        )
    )
    AND people.name != "Kevin Bacon" 
    GROUP BY people.name
    
    0 讨论(0)
  • 2020-12-11 14:30

    I found these steps helpful:

    1. Get the ID of Kevin Bacon, with the criteria that it's the Kevin Bacon who was born in 1958
    2. Get the movie IDs of Kevin Bacon using his ID (hint: linking his ID in table1 with table2)
    3. Get other stars' IDs with the same movie IDs
    4. Get the name of these stars, and exclude Kevin Bacon (because the spec says he shouldn't be included in the resulting list)

    Note: In the first line of your code, instead of COUNT(name), you can use SELECT name to get the people's names

    0 讨论(0)
  • 2020-12-11 14:31

    I solved this problem by using DISTINCT and NOT EQUAL operator in SQL.

    Here are the steps I took:

    SELECT DISTINCT(name)
    FROM people
    JOIN stars ON stars.person_id = people.id
    JOIN movies ON movies.id = stars.movie_id
    WHERE movies.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.name = "Kevin Bacon"
           AND people.birth = 1958)
      AND people.name != "Kevin Bacon";
    

    Result is 176 distinct rows of names.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题