SQL - Find the film title and language name of all films in which ADAM GRANT acted

后端 未结 2 1090
庸人自扰
庸人自扰 2021-01-26 07:20

I\'m having a ton of trouble thinking in terms of SQL on problems like this. I don\'t know how I should be structuring my queries. Should I be joining film on

相关标签:
2条回答
  • 2021-01-26 07:31

    You should use inner join

    SELECT 
       film.title
      , language.name
    FROM film
    INNER JOIN language on film.language_id = language.language_id
    INNER JOIN film_actor on film.film_id = film_actor.film_id
    INNER JOIN actor on actor.actor_id = film_actor.actor_id
    WHERE actor.first_name = 'ADAM'
    AND actor.last_name ='GRANT'
    
    0 讨论(0)
  • 2021-01-26 07:36

    You could use NATURAL JOIN, which removes the range variables (film_actor, actor) from the query, as well as the 'stating the blooming obvious' search conditions (actor_id = actor_id):

      SELECT title AS film_title, name AS language_name
        FROM film 
             NATURAL JOIN film_actor
             NATURAL JOIN actor
             NATURAL JOIN language
       WHERE first_name = 'ADAM'
             AND last_name ='GRANT';
    

    I'm not a great fan of the school of thought where a data element such as "name of language" is just called just name and the context language is supposed to come from the table name. What works well in Java classes doesn't necessarily translate to SQL, which is why I find I need to use a rename in the query (SELECT name AS language_name...). Additionally, you have a name attribute in category, and you would have problems if you ever had NATURAL JOINs including both language and category. Much better to have unique and consistent attribute names throughout your schema, as you have successfully done with actor_id, for example.

    0 讨论(0)
提交回复
热议问题