我写了一个查询,可以找到某个班级中某一性别的学生人数,但是我找不到一个方法来写一个查询,可以对两个性别都做同样的事情。我尝试了两次(针对每个性别)编写查询,然后使用union将它们组合起来,但都做不到。
查询#1
SELECT MAX(Class.Class_ID) AS Class_ID, COUNT(Enrolled_In.Reg_No) AS Male_Students
FROM Enrolled_In
INNER JOIN Class ON Enrolled_In.Class_ID = Class.Class_ID
INNER JOIN Student ON Enrolled_In.Reg_No = Student.Reg_No
WHERE Class.Class_ID = 'E-100' AND Student.Gender = 'M';
结果:
CLASS_ID MALE_STUDENTS
-------- -------------
E-100 2
查询#2
SELECT MAX(Class.Class_ID) AS Class_ID, COUNT(Enrolled_In.Reg_No) AS Female_Students
FROM Enrolled_In
INNER JOIN Class ON Enrolled_In.Class_ID = Class.Class_ID
INNER JOIN Student ON Enrolled_In.Reg_No = Student.Reg_No
WHERE Class.Class_ID = 'E-100' AND Student.Gender = 'F';
结果:
CLASS_ID FEMALE_STUDENTS
-------- ---------------
E-100 1
我希望输出是这样的,最好使用单个查询:
CLASS_ID MALE_STUDENTS FEMALE_STUDENTS
-------- ------------- ---------------
E-100 2 1
谢谢您。
2条答案
按热度按时间eqqqjvef1#
snz8szmq2#
请尝试以下操作