当前位置: 代码迷 >> Oracle开发 >> 怎么查询教师表,同时显示 教师姓名+教师的学生的个数
  详细解决方案

怎么查询教师表,同时显示 教师姓名+教师的学生的个数

热度:27   发布时间:2016-04-24 07:30:08.0
求助:如何查询教师表,同时显示 教师姓名+教师的学生的个数
教师表

1 张三
2 李四

学生表 
ID 姓名 老师ID
1 学生A 1
2 B 1
3 C 2

查询结果
ID 姓名 学生个数
1 张三 2
2 李四 1

感谢!膜拜

------解决方案--------------------
教师表 --teacher(teacherID,teacherName)
学生表 --student(StudentID,StudentName,teacherID)
SQL code
select tea.teacherName,Count(stu.StudentID) as StudentNum from student stuleft join teacher teaon tea.teacherID=stu.teacherIDgroup by teacherName
------解决方案--------------------
SQL code
create table tea(t_id number(10),t_name varchar2(20));insert into tea values(1,'张三');insert into tea values(2,'李四');create table stu(s_id number(10),s_name varchar2(20),t_id number(10));insert into stu values (1,'学生A',1);insert into stu values (2,'学生B',2);insert into stu values (3,'学生C',1);insert into stu values (4,'学生D',1);insert into stu values (5,'学生E',2);select a.t_id,a.t_name 姓名,count(b.s_id) 学生个数from tea a,stu bwhere a.t_id=b.t_idgroup by a.t_id,a.t_nameorder by a.t_idt_id  姓名   学生个数----------------------1      张三     32      李四     2
  相关解决方案