2013-06-29 3 views
0

나는 견인 테이블 '일치'및 '포럼'내가 포럼 테이블에 주석을 가진 일치 테이블에서 일치하는 정보를 얻을 필요가있다 그래서 나는 다음과 같은 쿼리를 사용sql에서 count를 올바르게 사용하는 방법?

SELECT distinct forum.match_static_id, matches.* 
from forum 
INNER JOIN matches 
    ON forum.match_static_id = matches.static_id 
WHERE forum.comments_yes_or_no = 1 

나는 점점 피하기 위해 별개의 사용을 포럼 테이블에 두 개 이상의 댓글이있는 경우 동일한 일치가 두 번 반복됩니다.

문제는 동일한 검색어로 각 경기의 댓글 수를 계산하고 싶습니다. 나는 다음을 사용한다 :

SELECT distinct forum.match_static_id, count(forum.comments), matches.* 
from forum 
INNER JOIN matches 
    ON forum.match_static_id = matches.static_i 
WHERE forum.comments_yes_or_no = 1 

그러나 그것은 나에게 하나의 레코드를 준다. 문제가 무엇입니까 ?? 그룹별로 사용해야합니까? 그리고 그렇다면이 붐비는 쿼리에서 어디까지?

+0

당신은, 바로 MySQL을 사용해야합니까? – Bohemian

+0

예 mysql을 사용합니다. – Basel

+0

'matches.static_id'는'marches'의 기본 키입니까? –

답변

0

이 시도하십시오

SELECT forum.match_static_id, count(matches.id), matches.* 
from forum 
INNER JOIN matches 
    ON forum.match_static_id = matches.static_i 
WHERE forum.comments_yes_or_no = 1 
GROUP BY forum.id 
관련 문제