计算来自两个不同表的记录数并将其分组

时间:2018-06-20 07:02:21

标签: mysql

表1:学生(姓名,注册号,部门,学院ID)
表2:学院(名称,学院ID ,部门,学院ID)

所需的o / p: 部门,学生人数,教师人数以表格形式出现

我一直在尝试的查询:

select 
    dept,
    count(distinct student.dept) as total, 
    count(distinct faculty.dept) as total1 
from student 
join faculty 
    on student.college_id=faculty.college_id
group by dept;

查询未提供任何输出。

在您帮助之后 现在查询

select * from (
(select count(*)as stu_count,student.department,student.college_id 
from student group by department)T1 
inner join 
(select count(*)as fact_count,faculty_per.department,faculty_per.college_id 
from faculty_per group by department)T2 
on T1.college_id=T2.college_id);

谢谢。

1 个答案:

答案 0 :(得分:0)

select * from ((select count(*)as stu_count,t2.dept,t2.college_id from Student group by dept)T1
inner join
(select count(*)as fact_count,t3.dept,t3.college_id from Facultygroup by dept)T2
on T1.college_id=T2.college_id);

可以通过对两个单独的查询执行内部联接来提取学生和教师的数量。不需要区分,因为我们正在按

分组