我有老师和学生的桌子 老师桌有
id teacherName classAssigned
1 tea1 1
2 tea2 1
3 tea3 3
4 tea4 2
学生表
id name class
1 st1 1
2 st2 1
3 st3 3
4 st4 4
我使用左连接查询... 正如
SELECT student1.name , teacher1.name
FROM student1 LEFT JOIN teacher1 ON student1.class = teacher1.class where student1.class=1
Student_name Teacher_name
St1 tea1
St1 tea2
St2 tea1
St2 tea2
现在我希望结果为
class TeacherNo Student_no
1 2 2
因为在班级1中有两名学生,两名学生。我们得到的数字为4
答案 0 :(得分:1)
使用COUNT(DISTINCT )
对学生和老师进行统计,如下所示:
SELECT
student1.class,
COUNT(DISTINCT student1.name) AS studentno ,
COUNT(DISTINCT teacher1.teacherName) AS Teacherno
FROM student1
LEFT JOIN teacher1 ON student1.class = teacher1.classAssigned
where student1.class = 1
GROUP BY student1.class;
请在此处查看:
这会给你:
| CLASS | STUDENTNO | TEACHERNO |
---------------------------------
| 1 | 2 | 2 |
答案 1 :(得分:0)
我的朋友尝试此查询,它将计算其班级= 1
的教师姓名和学生姓名SELECT class , Count(teacher1.name) as TeacherNo, Count(student1.name) as Student_no
FROM student1 LEFT JOIN teacher1 ON student1.class = teacher1.classAssigned where student1.class=1