表结构如下:
学校表:School 字段:SchoolID【学校ID】、SchoolName【学校名称】
学员表:Student 字段:StudentID【学员ID】、SchoolName【学员姓名】、
StudentAge【学员年龄】、SchoolID【对应表School.SchoolID】、
StudentSexID【学员性别(1代表男、2代表女)】。
页面里放一个DropDownList下拉框,显示所有学校(已做出,是用户控件),一个GridView统计(显示)每个学校 姓名相同人数、年龄相同人数、性别相同人数 。
当在DropDownList下拉框列表中选某个学校时,就相应统计某个学校的人。如果是不限就显示所有学员的记录。
这样的SQL语句该如何写?(不要存储过程,只要SQL语句)
SELECT StudentName,COUNT(*) AS StudentNameCount FROM Student GROUP BY StudentName HAVING COUNT(*) >1 ORDER BY StudentNameCount WHERE SchoolID=DropDownList的值
select distinct
(
select COUNT(StudentName) from T_Student st1
where st1.schoolID= st.SchoolID and st1.StudentName=st.StudentName
) as StudentCount
,
(
select COUNT(StudentAge) from T_Student st2
where st2.schoolID= st.SchoolID and st2.StudentAge=st.StudentAge
) as AgeCount
,
(
select COUNT(SexID) from T_Student st3
where st3.schoolID= st.SchoolID and st3.SexID=st.SexID
) as SexCount,
SchoolID
from T_Student st
where SchoolID=SelectedValue
group by SchoolID,StudentName,StudentAge,SexID
SelectedValue 是你选择的值