How to display all the columns of two different tables as ONE ?
I have two tables, movies and movie_actors in MovieDB. The id of the movies is primary key and id of the movie_actor is foreign key, and movies_actor.id is referenced to movies.id
TABLE MOVIES
id/title/director/genre/year_of_release
TABLE MOVIE_ACTOR
id/title/actor/age
I used Union, but its not working, because union is asking for same number of attributes. but here the first table has 4 columns and the second has only 3.
I would like to display as in one table
id/title/director/genre/year_of_release/actor/age
any ideas?
You will want to use a join on the tables. The join will be between the two fields that are primary/foreign keys to each other which you stated was the
idin themovietable and theidin themovie_actortable:If you are not familiar with join syntax than here is great visual explanation of joins.
An
INNER JOINwill return the set of records that match in both tables. If you had records in themovietable that did not have matching records in themove_actortable, then you would want to use aLEFT JOIN. This would return all movies even if it did not have records in themovie_actortable:See SQL Fiddle with Demo of both queries.