我正在尝试在SQL中进行查询。该数据库是IMBD之一。这是形式:
所以,我需要电影“低俗小说”(id = 2175869)的演员/女演员,这部电影从未在其他电影中与另一位与制片小说的演员/女演员共同演出。
应该是这样的:
SELECT person_id FROM cast_info WHERE movie_id = 2175869 AND
person_id NOT IN (SELECT )
但我无法弄明白如何做第二部分。
答案 0 :(得分:1)
查询后来用作子查询,返回除了Pulp Fiction以外的电影,其中不止一个Pulp Fiction的演员在其中播放
SELECT movie_id
FROM cast_info
WHERE
movie_id <> 2175869 AND
person_id IN (SELECT person_id FROM cast_info WHERE movie_id = 2175869)
GROUP BY movie_id
HAVING COUNT(*) > 1
现在让人们在纸浆小说中扮演从未参加过这样一部电影的人
SELECT person_id
FROM cast_info
WHERE
movie_id = 2175869 AND
person_id NOT IN (
SELECT person_id FROM cast_info WHERE movie_id IN (
SELECT movie_id
FROM cast_info
WHERE
movie_id <> 2175869 AND
person_id IN (SELECT person_id FROM cast_info WHERE movie_id = 2175869)
GROUP BY movie_id
HAVING COUNT(*) > 1
)
)
注意:我假设每部电影只能演一个演员一次。如果同一个演员可以在同一部电影中为不同的角色投放,则将COUNT(*)
替换为COUNT(DISTINCT person_id)
。
答案 1 :(得分:0)
首先,您需要看到纸浆小说中的所有演员:
SELECT person_id as pulp_actor_id
FROM cast_info
WHERE movie_id = 2175869
然后你需要找到所有演员二人组:
SELECT c1.person_id as actor1_id
c2.person_id as actor2_id
FROM cast_info c1
JOIN cast_info c2
ON c1.person_id < c2.person_id
WHERE c1.movie_id = 2175869
AND c2.movie_id = 2175869;
在纸浆小说旁找到所有二人组合的电影。
SELECT actor1_id, actor2_id, actor1_movies.movie_id
FROM (SELECT c1.person_id as actor1_id
c2.person_id as actor2_id
FROM cast_info c1
JOIN cast_info c2
ON c1.person_id < c2.person_id
WHERE c1.movie_id = 2175869
AND c2.movie_id = 2175869) as duos
JOIN cast_info as actor1_movies
ON duos.actor1_id = actor1_movies.person_id
AND actor1.movie_id <> 2175869
LEFT JOIN cast_info actor2_movies
ON duos.actor2_id = actor2_movies.person_id
AND actor1_movies.movie_id = actor2_movies.movie_id
WHERE actor2.movie_id IS NOT NULL;
现在让所有来自纸浆小说的演员不在最后的结果中
SELECT person_id as pulp_actor_id
FROM cast_info c
LEFT JOIN ( SELECT actor1_id, actor2_id, actor1_movies.movie_id
FROM (SELECT c1.person_id as actor1_id
c2.person_id as actor2_id
FROM cast_info c1
JOIN cast_info c2
ON c1.person_id < c2.person_id
WHERE c1.movie_id = 2175869
AND c2.movie_id = 2175869) as duos
JOIN cast_info as actor1_movies
ON duos.actor1_id = actor1_movies.person_id
AND actor1.movie_id <> 2175869
LEFT JOIN cast_info actor2_movies
ON duos.actor2_id = actor2_movies.person_id
AND actor1_movies.movie_id = actor2_movies.movie_id
WHERE actor2.movie_id IS NOT NULL;
WHERE movie_id = 2175869 ) as duos
ON c.person_id IN ( actor1_id, actor2_id )
WHERE duos.movie_id IS NULL