查詢最主要的就是確定查詢的主表,和輔助表。
這個的話我們可以確認是用子查詢。
主表就是我們的學生表: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 直接每個查出來的都是一條數據。