大家。 我是 sql 新手,只是想知道如何通过一项选择从表中选择所有具有两种职业的 id_people。
我的 table 是这样的
CREATE TABLE `films_peoples_prof` (
`id_film` int(11) NOT NULL,
`id_people` int(11) NOT NULL,
`id_profession` int(11) NOT NULL,
PRIMARY KEY (`id_film`,`id_people`,`id_profession`),
KEY `id_people` (`id_people`),
KEY `id_profession` (`id_profession`),
CONSTRAINT `films_peoples_prof_ibfk_1` FOREIGN KEY (`id_film`) REFERENCES `films` (`id`) ON DELETE CASCADE,
CONSTRAINT `films_peoples_prof_ibfk_2` FOREIGN KEY (`id_people`) REFERENCES `peoples` (`id`) ON DELETE CASCADE,
CONSTRAINT `films_peoples_prof_ibfk_3` FOREIGN KEY (`id_profession`) REFERENCES `professions` (`id`) ON DELETE CASCADE
)
+---------+-----------+---------------+
| id_film | id_people | id_profession |
+---------+-----------+---------------+
| 4 | 1 | 1 |
| 4 | 1 | 2 |
| 5 | 1 | 1 |
| 7 | 1 | 1 |
| 4 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 1 |
| 6 | 3 | 1 |
| 4 | 4 | 1 |
| 8 | 5 | 1 |
| 9 | 5 | 1 |
| 4 | 6 | 1 |
| 4 | 6 | 2 |
| 9 | 7 | 2 |
| 4 | 10 | 1 |
+---------+-----------+---------------+
id = 1 - Actor ,id = 2 - 制片人 在这种情况下结果应该是 1 , 6
最佳答案
您需要使用group by
,例如
select id_people
from films_peoples_prof
where id_profession in (1,2)
group by id_people
having count(distinct id_profession ) = 2;
关于mysql - sql 我如何选择 Actor 和制片人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44521555/