Skip to content
Advertisement

Getting wrong answer when trying to output a an actors roles

I am trying to figure out how I can get Daniel Radcliffe‘s career history. However, I am getting results which aren’t relevant to Daniel Radcliffe.


I know the that Daniel Radcliffe‘s ID is 1

I want to get the result as something similar to this, where there are NULL values on either the played_as or job column

Result I get: http://www.sqlfiddle.com/#!17/28dd0

Advertisement

Answer

Because you want to get one row for each played_as or job for each movie, you need to create a table with all the played_as and job values, which you can do with a UNION query. That query returns NULL for job when played_as is valid and vice versa. It can then be JOINed to the movie, person and famous_level tables to filter on the person_id and order by the level:

Output:

Demo on SQLFiddle

Note that you need to JOIN to famous_level on both movie_id and person_id to avoid getting extra rows in the output. Since not all movie/person combinations have a famous_level, you need to use a LEFT JOIN and COALESCE on the level value for when it is NULL.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement