2014-02-22 187 views
2

我有一個查詢,就像這樣:SQL查詢彙總多個查詢

SELECT A.director, COUNT(*) as NumActors 
FROM CanWorkWith A 
GROUP BY A.director 
HAVING COUNT(*) >= 5; 

的CanWorkWith表:

CanWorkWith(actor, director) 

這個查詢將返回唯一有ATLEAST 5名演員的導演,他們可以與...合作。

所以,現在我想取這個查詢的結果,並搜索另一個表來計算導演所擁有的電影數量。

所以,如果導演有5個演員,那麼我想搜索在表導演叫:

Movies (title, year, director, budget, earnings) 

然後終於,回到導演,和標題的總數是導演有,爲擁有5名演員以上的所有導演做這件事。

謝謝...

回答

2

只需在此結果和返回每個導演的電影數的查詢之間進行連接即可。

事情是這樣的:

SELECT 
    M.director, 
    COUNT(M.title) as 'Number of movies', 
    D.NumActors 
FROM Movies M 
    INNER JOIN (
     SELECT A.director, COUNT(*) as NumActors 
     FROM CanWorkWith A 
     GROUP BY A.director 
     HAVING COUNT(*) >= 5 
    ) D ON M.director = D.director 
GROUP BY M.director, 
     D.NumActors 
+0

是的,這就是它了,謝謝你啊! – Borovez

+0

@ user3015190如果它適合您,請不要忘記將答案upvote /標記爲正確。 –