mysql实战查询,mysql查询每门成绩的前十名?
学生表名为:student, 单科成绩的字段为:subject。学生名字为:name 查询单科成绩前十:
mysql语句为:
select * from student order by subject desc limit 10; 查询总分成绩前十:mysql语句为:
select sum(subject) subject,name from student group by name order by subject desc limit 10; 注: select sum(subject) subject,name 这句的意思是:sum(subject) subject 把单科成绩加总用subject 来命名(就是总成绩),name就是一个字段,这里只有两个字段。 group by name order by subject : group by name 的意思按照名字这一栏来分组,当然,学生成绩表名字有可能是一样的,按照学号是最准确的,这里只是举个例子。
order by subject 这句的意思是按照总分成绩排序,这里的subject 是前面重命名总分的意思。
select sum(subject) as countsubject,name from student group by name order by countsubject desc limit 10;