2011-08-28 8 views
0

의견이있는 테이블이 있습니다. 이 주석은 일대 다 관계, 즉 1 개의 질문에서 많은 의견을 통해 다른 질문 표와 관련됩니다. 이제 최대 5 건의 질문 목록을 원합니다 (연속적으로). 따라서 내 쿼리는 다음과 같이 반환해야합니다.MySql Query Help needed

Question Id:4 with 30 comments 
Question Id:2 with 27 comments 
Question Id:11 with 22 comments 
Question Id:5 with 15 comments 
Question Id:14 with 10 comments 

하나의 쿼리 또는 여러 쿼리를 통해이를 수행 할 수 있습니까? 그리고 어떻게?

답변

1

이 쿼리는 필요한 데이터를 가져옵니다. 출력 형식을 원하는대로 처리 할 수 ​​있습니다.

select questionid, count(commentid) as commentcount 
from question q 
inner join comment c on q.questionid = c.questionid 
group by questionid 
order by commentcount desc 
limit 5; 
+0

감사합니다. 나 해보자. – Blueboye