2012-02-15 2 views
0
select lastname, firstname 
    from person 
    group by lastname, firstname 
    having 50 >= 
       (select count(p.personid) from filmparticipation x, film f, person p 
       where f.filmid = x.filmid and 
       x.personid = p.personid and 
       x.parttype = 'cast' 
       ); 

짧은 소개, 동영상 데이터베이스를 기반으로합니다. 이 쿼리를 통해 나는 영화에 50 번 이상 등장하는 배우를 얻게되었습니다. 알아 두어야 할 점은, 영화에는 영화가 포함되어 있으며, 영화와 관련된 인물이있는 영화 관람가로 파싱됩니다. p.personid에는 성 및 이름이 들어 있습니다. 모든 지침이 preciated됩니다 :)SQL 카운터 오류

+1

그리고 무엇이 잘못 되었나요? – Blorgbeard

+0

카운터가 전혀 작동하지 않습니다 ... –

답변

1

을 당신 쿼리에 실제로 film 테이블이 필요하지 않으므로이 테이블을 남겨 둘 수 있습니다. 티. 또한 액터는 하나의 필름에서 여러 역할을 할 수 있으므로 테이블 당 filmparticipation 테이블에 여러 개의 항목이있을 수 있습니다. 이것을 잡으려면 DISTINCT의 하위 쿼리를 사용하십시오.

SELECT lastname, firstname, COUNT(*) AS films 
FROM (SELECT DISTINCT p.lastname, p.firstname, f.filmid 
    FROM person p, filmparticipation f 
    WHERE p.personid = f.personid AND f.parttype = 'cast') p 
GROUP BY lastname, firstname 
HAVING films >= 50; 
+0

PostgreSQL에서 다음과 같이 말하지 않습니다 : HAVING films> = 50 – user265767

2

이 주사를 ... 나는 서브 쿼리가 불필요 생각 :

select p.lastname, p.firstname, count(*) 
from person p 
join filmparticipation x on p.personid = x.personid 
join film f on x.filmid = f.filmid 
where x.parttype = 'cast' 
group by p.lastname, p.firstname 
having count(*) > 50 
0

(count) >= 50 

또는

50 <= (count)