key with multiple foreign keys - mysql

I have a database MOVIES and it has 2 tables. 1 is MOVIE and the other is GENRE.
I want to be able to have row entry of MOVIE to be associated with multiple rows in the GENRE table. Can this be done with only 1 column in the MOVIE table?
I have seen examples of movie databases, but they only allow 1 genre to be mapped to a single movie. Is there a way to have multiple genres belonging to a single entry movie? I'm using mySQL to create the database.

You should have a third table, MOVIE_GENRE that implements this many-to-many relationship. It has foreign keys into both the MOVIE and GENRE tables. A query to find list all the movies with their genres would look like:
SELECT m.title,
IFNULL(GROUP_CONCAT(g.name), "") genres
FROM Movie m
LEFT JOIN Movie_Genre mg ON m.id = mg.movie_id
JOIN Genre g ON g.id = mg.genre_id
GROUP BY m.id

You need a many-to-many or join table with 2 colums,
MovieID and GenreID
Having this structure will allow you to link a single movie to as many genres as you like

Related

How to find every genre from an artist in a music database in MYSQL?

So I've got a database with 4 tables: artist, genre, track and album. Track table points in a many to one way to Genre table, and so on with Track to Album, and Album to Artist.
Suppose I want to find every genre that 'John Coltrane' plays, so I thought about saying
SELECT DISTINCT Artist.name, Genre.name
FROM Artist
JOIN Genre
JOIN Album
JOIN Track
WHERE Artist.name = 'John Coltrane'
AND Track.genre_id = Genre.genre_id
But this just gives me
i.e. ALL genres jointed to John Coltrane, instead of just 'Jazz' which is what I'm seeking...
Why is it not working?
Since you didn't show all the table columns which would be used to join tables together. I will make some assumptions for you. I will be more accurate if I notice that you updated the topic and I will update my answer when I see it. Where I am incorrect about the column name that I join on just substitute the actual column with the one I put here and it will do what you are trying to do. ;-)
SELECT DISTINCT ART.name,
Genre.name
FROM Artist ART
JOIN Album ALB ON ART.artist_id = ALB.artist_id
JOIN Track TRK ON ALB.album_id = TRK.album_id
JOIN Genre G ON TRK.genre_id = G.genre_id
WHERE ART.name = 'John Coltrane'

denormalize many to many relationship in MySQL

I would like to denormalize many to many relationship in mysql. In order to import to MongoDB as Json format Schema.
Input
I have 3 tables:
Movies : id, title, url
Genres : id, genre
movie_genres : movie_id, genre_id
example
movie Table
id title link
1 star wars http://link-to-imdb
2 shrek http://link-to-imdb
movie_genres Table
movie genre
1 1
2 1
genres Table
id genre
0 unknown
1 action
2 comedy
3 drama
I would like to transform it to a single table by moving genres into movies as array or multiple values.
There are quite a few limited number of genres (only 15).
Output
So, Final output of table would be:
Movies : id, title, url, genre
Here, genre would be multiple values.
Example:
id title link genre
1 star wars http://link-to-imdb action, drama, sci-fi
2 shrek http://link-to-imdb anime
I did this - MySQL Query:
select M.id ,M.title ,M.release_date, M.video, M.IMDBURL, G.genre
from genres G, movie_genres MG, movies M
where M.id = MG.movie and MG.genre = G.id
but causes lot of repetition depending on number of genres. It would be nice If I could dump genres altogether.
In this cause you should use GROUP_CONCAT() function
SELECT movie.id, movie.title, movie.url, GROUP_CONCAT(g.genre SEPARATOR ', ') AS genres
FROM movie
LEFT JOIN movie_genres mg ON movie.id = mg.movie_id
LEFT JOIN genres g ON mg.genre_id = g.id
GROUP BY movie.id
I didn't test the query above (there could be some typos), but I hope you will be able to get the idea

SQL column with multiple values

i want to know how can we store multiple values in a sql column, or which is the best way to store the values in a sql column.
Consider in a movie table, a movie will be having multiple Genre
eg,
Genre: "Action, Adventure, Fantasy"
Genre: "Adventure, Drama, Fantasy, Thriller"
which is the best way to store the Genre values in database.
This is a classic n to m relation. It works like this
movies table
------------
id
name
...
genres table
------------
id
name
movie_genres table
------------------
movie_id
genre_id
The movie_genres table then contains one record for each genre of a movie. Example:
movie_genres
------------
movie_id | genre_id
1 | 1
1 | 2
1 | 13
To get the genres of a movie do:
select g.name as genre_name
from genres g
join movie_genres mg on mg.genre_id = g.id
join movies m on m.id = mg.movie_id
where m.name = 'star wars'
IMO, using a n:m relationship, as juergen_d suggested, is the best option.
But in mysql there is another option it might work in your case: using SET data type. Details here. Defintely not as powerful nor robust as using n:m relationship. Not normalization friendly.

How can I select the genres of a movie from a many-to-many genre2movie table

I have three tables movie, genre, movie2genre
genre has genreid and name
movie2genre has movieid and genreid
I want to write a query where I pass movieid and I get rows of the genres that movie has. How can I do this?
SELECT * FROM movie2genre
JOIN Genre on Genre.GenreId = movie2genre.GenreId
JOIN Movie on Movie.MovieId = movie2genre.MovieId
WHERE Movie.MovieId = [MovieId passed in]
Try this:
SELECT * FROM genre WHERE genreid IN(SELECT genreid FROM movie2genre WHERE movieid=[ID])
First, if you want to pass on something, you are looking at stored procedure.
Second, you need to join everything on ID's; You can refer to the image and later show us something what you tried.

Need MySQL Query ManyToMany Mapping Count Records

i have 3 tables (ManyToMany):
movie
moviegenre
genre
Structure of my tables:
movie:
id
name
...
genre:
id
name
moviegenre:
idMovie
idGenre
Movie is connected with genre via the moviegenre table (ManyToMany connection).
Now id like to know how many movies i have for genre (for example "Action")
Example result:<br/>
Action | 12<br/>
Horror | 9<br/>
Thriller| 3<br/>
...
Could you please help me creating this "simple" query? (MySQL 5.5.13)
Thank you very much! :D
SELECT G.name, count(MG.idMovie)
FROM MovieGenre MG
INNER JOIN Genre G on MG.idGenre = G.ID
GROUP BY G.name