简体   繁体   中英

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

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 film_actor and then join that on actor ? Or should I do it the other way around?

All I've been able to do is get a query that joins actor and film_actor with the name ADAM GRANT but I'm lost on where to go from here.

SELECT * FROM (actor JOIN film_actor ON actor.actor_id=film_actor.actor_id) WHERE first_name = 'ADAM' AND last_name = 'GRANT'

I've tried doing something to join these results with films, but I'm getting issues with my syntax.

SELECT title FROM (actor JOIN film_actor ON actor.actor_id=film_actor.actor_id) WHERE first_name = 'ADAM' AND last_name = 'GRANT' JOIN film ON film.actor_id = actor_id

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'

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 JOIN s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM