在同一个表中查找与给定的其他行具有相同的多对多关系的行

时间:2011-03-29 13:04:02

标签: mysql

以下是表格结构。

  • tbl_movie_master(movie_id,title)
  • tbl_genre_master(genre_id,genre)
  • tbl_movie_genre(movie_id,genre_id)

一部电影可以映射多种类型,因此tbl_movie_genre的情况下存在多对多关系。

我有电影详情页面,我正在获取电影ID。我必须编写查询来执行以下任务。

我必须为给定的电影ID找到具有所有匹配类型的电影。

例如我有电影ID 45,这部电影属于类型喜剧和浪漫,然后我必须找到所有其他类型喜剧和浪漫的电影。不应该填充只有喜剧或只有浪漫的电影。

是否可以在单个查询中执行此操作?

更新

在以下示例中,只有前2部电影符合标准。 “Hum tum ke pyar”和“housefull”。

drop table if exists tbl_movie_master, tbl_genre_master, tbl_movie_genre ;

create table tbl_movie_master (movie_id int, title varchar(100));
insert into tbl_movie_master values (1, 'Hum tum ke pyar'), (2, 'housefull'), (3, 'ek vakta ke liye'), (4, 'chalo pyar kare');

create table tbl_genre_master (genre_id int, genre varchar(100));
insert into tbl_genre_master values (1, 'horror'), (2, 'remoance'), (3, 'suspense'), (4, 'social');

create table tbl_movie_genre (movie_id int, genre_id int);
insert into tbl_movie_genre values (1, 1), (1, 2), (2, 1), (2, 2), (3, 3), (4, 1), (4,4);

mysql> select * from tbl_movie_master;
+----------+------------------+
| movie_id | title            |
+----------+------------------+
|        1 | Hum tum ke pyar  | 
|        2 | housefull        | 
|        3 | ek vakta ke liye | 
|        4 | chalo pyar kare  | 
+----------+------------------+
4 rows in set (0.00 sec)

mysql> select * from tbl_genre_master;
+----------+----------+
| genre_id | genre    |
+----------+----------+
|        1 | horror   | 
|        2 | remoance | 
|        3 | suspense | 
|        4 | social   | 
+----------+----------+
4 rows in set (0.00 sec)

mysql> select * from tbl_movie_genre;
+----------+----------+
| movie_id | genre_id |
+----------+----------+
|        1 |        1 | 
|        1 |        2 | 
|        2 |        1 | 
|        2 |        2 | 
|        3 |        3 | 
|        4 |        1 | 
|        4 |        4 | 
+----------+----------+
7 rows in set (0.00 sec)


SELECT m.*
FROM tbl_movie_master m
JOIN tbl_movie_genre mg ON mg.movie_id = m.movie_id
WHERE mg.genre_id = ALL (SELECT mg.genre_id FROM tbl_movie_genre mg WHERE mg.movie_id = 1);

Empty set (0.00 sec)

SELECT m.title 
FROM tbl_movie_master m
JOIN tbl_movie_genre allgenres 
  ON m.movie_id= allgenres.movie_id
JOIN 
  (SELECT genre_id FROM tbl_movie_genre WHERE movie_id=1) somegenres
  ON somegenres.genre_id=allgenres.genre_id;

+-----------------+
| title           |
+-----------------+
| Hum tum ke pyar | 
| Hum tum ke pyar | 
| housefull       | 
| housefull       | 
| chalo pyar kare | 
+-----------------+
5 rows in set (0.00 sec)

SELECT  M.`title`, G.`genre`, M2.`title`
FROM    tbl_movie_master AS M, tbl_movie_genre AS A
LEFT JOIN tbl_movie_master AS M2 ON A.`movie_id` = M2.`movie_id`
LEFT JOIN tbl_genre_master AS G ON A.`genre_id` = G.`genre_id`
WHERE   M.`movie_id` = 1;

+-----------------+----------+------------------+
| title           | genre    | title            |
+-----------------+----------+------------------+
| Hum tum ke pyar | horror   | Hum tum ke pyar  | 
| Hum tum ke pyar | remoance | Hum tum ke pyar  | 
| Hum tum ke pyar | horror   | housefull        | 
| Hum tum ke pyar | remoance | housefull        | 
| Hum tum ke pyar | suspense | ek vakta ke liye | 
| Hum tum ke pyar | horror   | chalo pyar kare  | 
| Hum tum ke pyar | social   | chalo pyar kare  | 
+-----------------+----------+------------------+
7 rows in set (0.00 sec)

4 个答案:

答案 0 :(得分:2)

SELECT m.*
FROM movie m
JOIN movie_genre mg ON mg.movie_id = m.id
WHERE mg.id = ALL (SELECT mg.genre_id FROM movie_genre mg WHERE mg.movie_id = ?)

?是给定的电影ID

答案 1 :(得分:1)

尝试:

SELECT m.title 
FROM tbl_movie_master m
JOIN tbl_movie_genre allgenres 
  ON m.movie_id= allgenres.movie_id
JOIN 
  (SELECT genre_id FROM tbl_movie_genre WHERE movie_id=45) somegenres
  ON somegenres.genre_id=allgenres.genre_id;

答案 2 :(得分:0)

这个问题为JOINs大声呼喊。

这样的事情会起作用:

SELECT  M.`title`, G.`genre`, M2.`title`
FROM    tbl_movie_master AS M, tbl_movie_genre AS A
LEFT JOIN tbl_movie_master AS M2 ON A.`movie_id` = M2.`movie_id`
LEFT JOIN tbl_genre_master AS G ON A.`genre_id` = G.`genre_id`
WHERE   M.`movie_id` = A.`movie_id`
            AND M.`title` = "Your Movie";

答案 3 :(得分:0)

需要在dnagirl的答案中添加“group by”以获得完全相同的类别数量。我想这就是我要找的......

SELECT mg1.movie_id,
       mm1.title,
       COUNT(*) AS cnt
FROM   tbl_movie_genre AS mg1
       INNER JOIN tbl_movie_master AS mm1
         ON mg1.movie_id = mm1.movie_id
       INNER JOIN (SELECT genre_id
                   FROM   tbl_movie_genre
                   WHERE  movie_id = 1) AS tt1
         ON mg1.genre_id = tt1.genre_id
GROUP  BY movie_id
HAVING cnt = (SELECT COUNT(*) AS mycnt
              FROM   tbl_movie_genre
              WHERE  movie_id = 1);