gpt4 book ai didi

MySQL - 一个表,多行具有重复的id

转载 作者:行者123 更新时间:2023-11-29 04:28:04 26 4
gpt4 key购买 nike

所以我有一个“流派”表,其中包含重复的流派和一个 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”,因为它既有喜剧又有音乐(假设同时查询“喜剧”和“音乐”)。

最佳答案

使用

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';

关于MySQL - 一个表,多行具有重复的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7411927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com