我有一个包含两列的表:actors和movies,如下所示:
ACTOR | MOVIE
-------+-------------
Volta | Pulp Fiction
Bruce | Pulp Fiction
Rhame | Pulp Fiction
Walke | Pulp Fiction
Rhame | Bad Movie
Bruce | Bad Movie
Volta | Decent Movie
Brian | Decent Movie
Walke | Awesome Movie
Brian | Awesome Movie
我想知道哪些演员,谁出现在低俗小说,从来没有出现在另一个电影与另一个演员从低俗小说。
从本例中,输出应为:
Volta
Walke
因为他们分别出现在低俗小说和正派电影以及令人敬畏的电影中,没有任何其他演员来自低俗小说。
我在用mysql。
最佳答案
SELECT m.actor
FROM movies m
WHERE
m.movie = 'Pulp Fiction'
AND NOT EXISTS
(
SELECT 1
FROM movies m1
JOIN movies m2 ON m1.movie = m2.movie
AND m1.actor <> m2.actor
AND m2.movie <> 'Pulp Fiction'
AND m2.actor IN (SELECT actor FROM movies WHERE movie = 'Pulp Fiction')
WHERE
m.actor = m1.actor
)
根据chrisprosser所做的SQLFiddle,它应该给出正确的结果。
关于mysql - SQL - Actor 之间的关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19136641/