简体   繁体   中英

MySQL Searching in Many-to-Many

I've begun working on my first MySQL database, and I've run into a simple problem.

I have my movies categorized by genre in a 'many-to-many' relationship:

'movies' Table
+---------+-------------------+
|movie_id |movie_title        |
+---------+-------------------+
|1        |Indiana Jones      |
|2        |Shaun of the Dead  |
+---------+-------------------+

'genres' table
+---------+-----------+
|genre_id |genre_name |
+---------+-----------+
|1        |adventure  |
|2        |comedy     |
|3        |horror     |
+---------+-----------+

'movie_genres' table
+---------+---------+
|movie_id |genre_id |
+---------+---------+
|1        |1        |
|2        |2        |
|2        |3        |
+---------+---------+

What I am trying to do is search my movies table by genre, and display all the genres each movie is in, for instance:

Searching 'horror'

+------------------+---------------+
|movie_title       |genre_names    |
+------------------+---------------+
|Shaun of the Dead |comedy, horror |
+------------------+---------------+

Here is a query I would use to search for horror movies

SELECT m.movie_title, GROUP_CONCAT(g.genre_name SEPARATOR ', ') as genre_names
    FROM movies m
    LEFT JOIN genre_movies gm ON gm.movie_id = m.movie_id
    LEFT JOIN genres g ON g.genre_id = gm.genre_id
    GROUP BY m.movie_id
    HAVING genre_names LIKE '%horror%'

The problem I have is that this query retrieves the entirety of all the tables before filtering it, which I gather is very inefficient. Is there a better way I could search by one genre while showing all associated genres?

Try this:

SELECT m.movie_title, A.genre_names
FROM movies m
INNER JOIN (SELECT gm.movie_id, GROUP_CONCAT(g.genre_name SEPARATOR ', ') AS genre_names
    FROM genre_movies gm INNER JOIN genres g ON g.genre_id = gm.genre_id
    GROUP BY gm.movie_id) AS A ON m.movie_id = A.movie_id 
WHERE genre_names LIKE '%horror%'

Why don't you save the genre_id in the movie-table?

something like this:

'movies' Table
+---------+------------+---------------------+
|movie_id |movie_genre |movie_title          |
+---------+----------------------------------+
|1        |1           | Indiana Jones       |
|2        |2 ,3        | Shaun of the Dead   |
+---------+----------------------------------+

So you just need 2 tables then. And you can easily filter the data in the query.

What I am trying to do is search my movies table by genre, and display all the genres each movie is in, for instance:

Could be something like this:

SELECT fields FROM table WHERE movie_genre LIKE %movie_genre_id%
SELECT m.movie_title, GROUP_CONCAT(g.genre_name SEPARATOR ', ') as genre_names
FROM  movies m
      INNER JOIN
      (
        SELECT gm.movie_id
        FROM   genre_movies gm 
               INNER JOIN genres g 
                  ON g.genre_id = gm.genre_id
        WHERE  g.genre_name = 'horror'
        GROUP BY gm.movie_id
      ) a ON a.movie_id = m.movie_id
      INNER JOIN genre_movies gm ON gm.movie_id = m.movie_id
      INNER JOIN genres g ON g.genre_id = gm.genre_id
GROUP BY m.movie_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM