MySQL - 一个表,多行重复id

时间:2011-09-14 06:05:17

标签: mysql

所以我有一个“流派”表,它有重复的流派和一个id,我想制定一个查询,以便我返回所有具有特定id的流派的行。

例如,

+---------+-------+
| genre   | movie |
+---------+-------+
| Musical |   558 |
| Musical |   562 |
| Musical |   597 |
| Musical |   651 |
| Musical |   656 |
| Musical |   791 |
| Musical |   810 |
| Musical |   845 |
| Musical |   859 |
| Musical |   919 |
| Musical |   949 |
| Musical |   971 |
+---------+-------+
12 rows in set (0.00 sec)

+--------+-------+
| genre  | movie |
+--------+-------+
| Comedy |   642 |
| Comedy |   643 |
| Comedy |   644 |
| Comedy |   651 |
| Comedy |   654 |
| Comedy |   658 |
+--------+-------+
6 rows in set (0.00 sec)

我希望它能回归“电影651”,因为它既有喜剧也有音乐(假设对“喜剧”和“音乐剧”都有疑问)。

3 个答案:

答案 0 :(得分:4)

使用

SELECT DISTINCT A.MOVIE FROM GENRES A, GENRES B WHERE A.MOVIE = B.MOVIE AND A.GENRE = 'Comedy' AND B.GENRE = 'Musical';

编辑 - 根据评论:

SELECT DISTINCT A.MOVIE 
FROM
    GENRES A 
  INNER JOIN
    GENRES B
        ON A.MOVIE = B.MOVIE
WHERE A.GENRE = 'Comedy'
  AND B.GENRE = 'Musical';

答案 1 :(得分:1)

 SELECT DISTINCT
    movie
 FROM
    genres
 WHERE
    movie in (SELECT movie from genres where genre = "Musical")
    AND movie in (SELECT movie from genres where genre = "Comedy")

答案 2 :(得分:0)

这将为您提供

中的ID列表
select movie from table a join table b on a.movie = b.movie