统计一下学校里的总人数



学校数据库他们有两个主表学生和老师表的创建几乎与类似

创建表学生(students_ int,年int)创建表格教师(Teacher_id int,年int)

但我每年要计算总人数(学生+教师(。

示例

学生students_id年2020年1月2019年2月2020年3月2019年4月教师教师id年份2020年1月2018年2月2020年3月2019年4月答复年度总计2020 4(2名学生+2名教师(2019 3(2名学生+1名教师(2018 1(0名学生+1名教师(

使用union all和aggregation:

select year, count(*)
from ((select year from students) union all
(select year from teachers)
) st
group by year;

最新更新