简体   繁体   English

MySQL在多对多中搜索

[英]MySQL Searching in Many-to-Many

I've begun working on my first MySQL database, and I've run into a simple problem. 我已经开始研究我的第一个MySQL数据库了,我遇到了一个简单的问题。

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? 为什么不在电影表中保存genre_id?

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. 所以你只需要2个表。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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