以下是两个相关的表格
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])));
答案 0 :(得分:1)
最简单的方法是进行汇总并使用top
和order by
:
select top 1 school_id
from student
group by school_id
order by COUNT(*) desc