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
Related
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
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.
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.
I've a table named 'artist' with columns - id, name, genre
I need to find out all artist that share the same genre.
Here's an example: If artist X has genre ‘rock’ and ‘classic’, artist Y has genre ‘classic’ and ‘pop’, and artist Z has genre ‘pop’, then sample output as below:
first artist | second artist | genre
X | Y | Classic
Y | Z | Pop
Join data using Genre
Exclude "mirror duplicates" (XY-Classic vs YX-Classic) and loops (
ZZ-Pop ) using S.id < J.id condition:
Select S.Name as FirstArtist,
J.Name as SecondArtist,
S.Genre as CommonGenre
From Artist S
Inner Join Artist J
ON S.Genre = J.Genre and S.id < J.id
Judging from your sample output, you're trying to do two things. First, order your data set, and second, "pivot" it so some columns in your data become rows.
The first part:
SELECT name, genre
FROM artist
ORDER BY genre
The second part is presentation. There are many ways to do this. You could try this, for example.
SELECT GROUP_CONCAT(name ORDER BY name) AS names,
genre
FROM artist
GROUP BY genre
ORDER BY genre
Or you could look up various techniques in MySQL to do pivoting. Hint: they're all kind of difficult.
Or, you could do this in your presentation software.
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