0

以下是两个相关表

CREATE TABLE SCHOOL
(School_ID char(3), School_Name char(15), School_Address char(15), School_City char(15), School_State char(2), School_Zip char(5), School_Type char(15));
CREATE TABLE STUDENT
(School_ID char(3), St_LastName char(15), St_FirstName char(15), St_DOB char(15), St_SS# char(15), St_Address char(15),St_City char(15), St_State char(2),st_Zip char(5), st_phone char(15));

我试图在访问中编写查询:显示最多学生的学校 ID 但我的查询显示学生姓名与最大 School_ID 而不是出现次数。请帮助我

查询如下:

SELECT STUDENT.School_ID, SCHOOL.School_Name, STUDENT.St_LastName, STUDENT.St_FirstName
FROM (SELECT MAX(STUDENT.School_ID) as max
FROM SCHOOL INNER JOIN STUDENT ON (SCHOOL.School_ID = STUDENT.School_ID) AND (SCHOOL.School_ID = STUDENT.School_ID)
)  AS Overallcount, SCHOOL INNER JOIN STUDENT ON (SCHOOL.School_ID = STUDENT.School_ID) AND (SCHOOL.School_ID = STUDENT.School_ID)
WHERE (((STUDENT.School_ID)=([Overallcount].[max])));
4

1 回答 1

1

最简单的方法是进行聚合并使用topand order by

select top   1    school_id
from student
group by school_id
order by COUNT(*) desc
于 2013-03-20T13:53:33.140 回答