查询最主要的就是确定查询的主表,和辅助表。

这个的话我们可以确认是用子查询。

主表就是我们的学生表:tblstudent可以查到学生的学号,姓名

辅助表就是成绩表,tblscore

自己写的语句如下:

SELECT  tblstudent.StuId,tblstudent.StuName,xuankeshu.kscount,kccj.countscore  FROM TBLSTUDENT ,
(
SELECT  count(tblscore.CourseId) kscount,tblscore.StuId sidd FROM tblscore GROUP BY tblscore.stuid 
) xuankeshu,-- 这里查出的是学生的选课数量,以学生的学号分组
(

select  sum(tblscore.Score) countscore,tblscore.StuId siddd from tblscore Group by tblscore.stuid  
) kccj -- 这里查出的是学生的总成绩,以学生的学号分组
where TBLSTUDENT.StuId=xuankeshu.sidd and TBLSTUDENT.stuid=kccj.siddd

 

答案的做法是:

 Select StuId,StuName,
  (Select Count(CourseId) From tblScore t1 Where t1.StuId=s1.StuId)SelCourses,
 (Select Sum(Score) From tblScore t2 Where t2.StuId=s1.StuId)  SumScore 
   From tblStudent s1

三个select 直接每个查出来的都是一条数据。

 

相关文章:

  • 2021-12-28
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案