gpt4 book ai didi

sql - SQL Server中如何统计每位老师的学生和类(class)?

转载 作者:行者123 更新时间:2023-12-04 19:22:01 24 4
gpt4 key购买 nike

假设有 3 个这样的表:

老师:

teacher_id, name, ... 

学生:
student_id, teacher_id, ... 

类(class):
class_id, teacher_id, ... 

是否有一个 SQL 查询来获取每个老师分配了多少学生和类(class)?

结果应该是这样的:
teacher_id  name    students    classes 
t_001 AAA 3 2
t_002 BBB 1 2
...

最佳答案

尝试这样的事情:

SELECT 
t.teacher_id, t.name,
Classes = (SELECT COUNT(*) FROM Class c WHERE c.teacher_id = t.teacher_id),
Students = (SELECT COUNT(*) FROM Student s WHERE s.teacher_id = t.teacher_id)
FROM teacher t

关于sql - SQL Server中如何统计每位老师的学生和类(class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11914305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com