2014-11-23 6 views
0

내가해야 할 일은 다른 테이블의 열에서 특정 값이 가장 많이 발생하는 행을 하나의 테이블에서 가져와야한다는 것입니다.열의 값이 다른 테이블에서 가장 많이 발생하는 테이블에서 행을 가져 오는 방법은 무엇입니까?

-------------------------------------------- 
|schoolid|schoolname|schoolstate|schoolcity| 
-------------------------------------------- 
| 1  | school a | New York | New York | 
| 2  | school b | California|Las Angeles| 
| 3  | school c | Texas  | Dallas | 
-------------------------------------------- 

----------------------------------------- 
|studentid|studentname|studentschool|gpa| 
----------------------------------------- 
| 1  | John Doe | school a |3.1| 
| 2  | John Doe | school c |1.7| 
| 3  | John Doe | school b |2.8| 
| 4  | John Doe | school a |3.9| 
| 5  | John Doe | school a |3.0| 
----------------------------------------- 

내가 선택하고 각 학교에서 학생들의 양에 의해 학교를 주문해야, 내 질문을 바꾸어 말하다하려면 다음은 두 테이블의 구조는 다음과 같다.

+2

힌트 (당신이에 더 학생이없는 학교에 필요하지 않은 경우) 당신은 사용할 수 있습니다 : 이것은 오히려 기본 쿼리가 일반적으로 것입니다 'JOIN','GROUP BY'와'ORDER BY'를 포함합니다. –

답변

1

당신은

select s.schoolname, (select count(s1.schoolid) from school s1 ,students st1 
where s1.schoolname=st1.studentschool and st1.studentschool=s.schoolname) 
studentCount from school s order by studentCount desc; 

사용할 수 있습니다 또는

select s1.schoolname,count(*)studentcount from school s1 ,students st1 
where s1.schoolname=st1.studentschool GROUP BY s1.schoolname order by studentcount desc; 
관련 문제