因此,我有一个“genres”表,其中包含重复的genres和一个id,我想构造一个查询,以便返回具有两个genres的特定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)
我希望它返回“movie 651”,因为它在喜剧和音乐剧中都有(假设同时查询了“喜剧”和“音乐剧”)。
最佳答案
使用
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';