查詢 mysql 用戶,mysql查詢每門成績的前十名?
學(xué)生表名為:student, 單科成績的字段為:subject。學(xué)生名字為: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 的意思按照名字這一欄來分組,當(dāng)然,學(xué)生成績表名字有可能是一樣的,按照學(xué)號是最準(zhǔn)確的,這里只是舉個例子。
order by subject 這句的意思是按照總分成績排序,這里的subject 是前面重命名總分的意思。
select sum(subject) as countsubject,name from student group by name order by countsubject desc limit 10;