SQL实现输出每个专业年龄最大、最小的学生姓名及其年龄
现有3张表格,Student,KC,XS_KC,分别代表学生信息表,课程信息表以及学生成绩表,而Student中有每个学生专业、年龄信息,现需要输出其年龄最大、最小的学生姓名和年龄
实现方法
代码如下:
select a.姓名,a.专业名,(2020-year(a.出生日期)) as 年龄
from Student a,(select min(出生日期) 年龄,专业名 from Student group by 专业名) b
where a.专业名=b.
专业名 and a.出生日期 = b.年龄
union all
select a.姓名,a.专业名,(2020-year(a.出生日期)) as 年龄
from Student a,(select max(出生日期) 年龄,专业名 from Student group by 专业名) b
where a.专业名=b.专业名 and a.出生日期 = b.年龄
order by 专业名
分别求取各专业年龄最大与年龄最小的学生信息,然后将表合并,并按专业名排序。其结果如下: