I have three tables(simplified)
movie(id int primary key identity, title varchar(20) not null)
genre(id int primary key identity, type varchar(10) not null)
movie_genre(movie_id int references movie(id),
genre_id int references genre(id),
primary key(movie_id, genre_id))
Data in movie
id title
---------------------------
1 | Inception
2 | The Dark Knight
Data in genre
id type
---------------------
1 | action
2 | adventure
3 | thriller
Data in movie_genre
movie_id genre_id
----------------------------
1 | 1
1 | 2
2 | 1
2 | 3
I want to display movie name with its genre types displayed in one column. So, the output would be
title | genres
-----------------------------------------
Inception | action adventure
The Dark Knight | action thriller
I tried to do it in this way
select
movie.title, genre.type
from
movie, genre
where
movie.id = movie_genre.movie_id
and genre.id = movie_genre.genre_id;
but it says :
The multi-part identifier "movie_genre.movie_id" could not be bound.
The multi-part identifier "movie_genre.genre_id" could not be bound.
I am very new to SQL, any help would be appreciated.
Edit :
Using
SELECT G.[Type] ,M.[Title]
FROM movie_genre MG
LEFT JOIN genre G ON MG.genre_id = G.ID
LEFT JOIN movie M ON MG.Movie_ID = M.ID
OR
select movie.title, genre.type
from movie, genre, movie_genre
where
movie.id = movie_genre.movie_id
and genre.id = movie_genre.genre_id;
The output is now,
title | genres
-----------------------------------------
Inception | action
Inception | adventure
The Dark Knight | action
The Dark Knight | thriller
How could I display genres in one row?
{ }) on the editor toolbar to nicely format and syntax highlight it!JOINsyntax in the ANSI-92 SQL Standard (more than 20 years ago) and its use is discouraged