本文介绍了MySQL获得最高平均分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写mysql查询以返回平均课程评分最高的前3门课程.我有两个表格,评分和课程.
I am trying to write a mysql query to return the top 3 courses that have the highest average course rating. I have two tables, Ratings and Courses.
收视率表:
courseId rating
1 6
2 2
1 4
2 5
3 3
4 0
6 0
课程表:
courseId cnum cname
1 100 name1
2 112 name2
3 230 name3
4 319 name4
5 122 name5
6 320 name6
我需要返回平均评分最高的前三门课程.有什么想法我可以做到这一点吗?谢谢
I need to return the top 3 courses that have the highest average rating. Any ideas how I could do this? Thanks
推荐答案
SELECT Courses.*
FROM Courses NATURAL JOIN (
SELECT courseId, AVG(rating) avg_rating
FROM Ratings
GROUP BY courseId
ORDER BY avg_rating DESC
LIMIT 3
) t
在 sqlfiddle 上查看.
这篇关于MySQL获得最高平均分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!