select * from (select id,name,(select sum(score) from SCORES t2 where t2.studentid=t1.id) as totalscorefrom STUDENT t1group by id,nameorder by totalscore desc) t3where t3.rownum <=3学生表(student):id name class 科目表(subject):id name成绩表:studentid , subjectid ,score 用Oracle 编写一个sql 查出学生的 id name 和分数(总成绩)取前三名学生select t1.id,t1.name,t3.totalscorefrom STUDENT t1,(select t2.studentid,sum(score) as totalscore from score t2 group by studentid order by totalscore desc)t3where t1.id = t3.studentidand rownum<=3;
将子查询放在from语句中只执行一次,比放在select语句中..效率高多了