我正在嘗試在SQL中執行查詢。數據庫是IMBD之一。這是形式:使用複雜子查詢在SQL中練習
所以,我需要從電影低俗小說(ID = 2175869),演員/女演員,從來沒有與低俗小說的另一位演員/女演員沒有其他影片共同擔任。
它應該是這樣的:
SELECT person_id FROM cast_info WHERE movie_id = 2175869 AND
person_id NOT IN (SELECT)
但我無法弄清楚如何做的第二部分。不是低俗小說等
我正在嘗試在SQL中執行查詢。數據庫是IMBD之一。這是形式:使用複雜子查詢在SQL中練習
所以,我需要從電影低俗小說(ID = 2175869),演員/女演員,從來沒有與低俗小說的另一位演員/女演員沒有其他影片共同擔任。
它應該是這樣的:
SELECT person_id FROM cast_info WHERE movie_id = 2175869 AND
person_id NOT IN (SELECT)
但我無法弄清楚如何做的第二部分。不是低俗小說等
查詢後用作子查詢,返回電影哪裏低俗小說不止一個演員在其中扮演
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(DISTINCT person_id)
替換COUNT(*)
。
檢查,這是一個非常大的數據庫,所以它需要時間,但它運行正確知道,我會遲早發佈結果。謝謝! –
首先,你需要看到低俗小說所有的演員:
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
謝謝,我會試試它,但這需要時間,我會向你發佈結果。 –
Hola Jaime,Check Olver的答案,比我的簡單。 –
Muy honesto por tu parte Juan Carlos,gracias,voy a ello –
@Sami我不是很肯定它。你是指@Harshil一樣嗎? –
是的,他指的是和我一樣的問題 –
Okey,謝謝,我現在正在學習 –