5. 資料庫題(以個人熟悉資料庫為準、按要求寫出sql) (1) 計算每個人的總成績並排名(要求顯示欄位:學號,姓名,總成績) (2) 計算每個人單科的最高成績(要求顯示欄位: 學號,姓名,課程,最
阿新 • • 發佈:2018-12-18
5. 資料庫題(以個人熟悉資料庫為準、按要求寫出sql) (1) 計算每個人的總成績並排名(要求顯示欄位:學號,姓名,總成績) (2) 計算每個人單科的最高成績(要求顯示欄位: 學號,姓名,課程,最高成績) (3) 列出各門課程成績最好的學生(要求顯示欄位: 學號,姓名,課程,成績) [分值:5]
use db2; create table t_student_score( stuid int(11), name varchar(50), subject varchar(50), score int(11) ) delete from t_student_score; select * from t_student_score; insert into t_student_score values(10001,'張三','語文',89); insert into t_student_score values(10001,'張三','數學',95); insert into t_student_score values(10001,'張三','外語',70); insert into t_student_score values(10001,'李四','語文',95); insert into t_student_score values(10001,'李四','數學',80); insert into t_student_score values(10001,'李四','外語',75); insert into t_student_score values(10001,'王五','語文',85); insert into t_student_score values(10001,'王五','數學',90); insert into t_student_score values(10001,'王五','外語',70); select stuid 學號, name 姓名,sum(score) 總成績 from t_student_score group by name order by 總成績 ; select t1.stuid 學號,t1.name 姓名,t1.subject 課程,t1.score 成績 from t_student_score t1, (select name,max(score) 最高成績 from t_student_score group by name)t2 where t1.name=t2.name and t1.score=t2.最高成績; select t1.stuid 學號,t1.name 姓名,t1.subject 課程,t1.score 成績 from t_student_score t1, (select subject,max(score) 最高成績 from t_student_score group by subject) t2 where t1.subject=t2.subject and t1.score=t2.最高成績;