现有两个表,student和course,student表中有学生编号sn字段(主键),学生名sname字段,course中有课程代号cn字段(主键)和课程名cname字段,现在要建一个表显示学生的选课情况,还要完成以下查询:
1:根据学生名查出该学生所选的课程。
2:根据课程名查出所有选这门课的学生。
3:选出选课最多的学生名。
4:选出最多人报名的课程名。
5:显示课程名的选课排行榜,按照报名的人数。
以上5条要求用sql语句写出。
建一个学生,课程对照表就行了s_t表
sn cn 关键字为sn+cn
1.select t3.cname from student t1,course t2,s_t t3 where
t1.sn=t2.sn and t2.cn=t3.cn and t1.sname=@学生名
2.select t1.sname from student t1,course t2,s_t t3 where
t1.sn=t2.sn and t2.cn=t3.cn and t3.cname=@课程名
3。select t1.sname from
(select a.sn,max(a.maxcourse) from
(select t3.sn,count(t3.cn) as maxcourse from s_t t3 group by t3.sn) a
group by a.sn) b ,student t1 where t1.sn=b.sn
4. select t2.cname from
(select a.cn,max(a.maxstudent) from
(select t3.cn,count(t3.sn) as maxstudent from s_t t3 group by t3.cn) a
group by a.sn) b ,course t2 where t2.cn=b.cn
5.select t2.cname,a.maxstudent as 人数 from
(select t3.cn,count(t3.sn) as maxstudent from s_t t3 group by t3.cn) a,course t2
order by a.maxstudent