我需要做的是我需要从一个表中获取行,其中另一个表中的列中的某个值最多。以下是2个表格的结构:
--------------------------------------------
|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|
-----------------------------------------
要重新提出问题,我需要根据每所学校的学生数量选择和订购学校。
答案 0 :(得分: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;