我有一个名为RATING的mysql表,其结构如下:
ID,USERNAME,RATING,RATER,MONTH
我希望能够按照当月的平均评分显示用户排行榜。
例如:
1,Bob,10岁,Rita,JUL2,Bob,8岁,Sue,JUL
3,Rita,9岁,Bob,JUL 4,Sue,4,Rita,JUL5,丽塔,10岁,苏,JUL
我想要一个可以产生这些结果的SQL查询:
丽塔:9.5 鲍勃:9 苏:4:有什么想法吗?
非常感谢
奥利弗。
答案 0 :(得分:2)
假设月份列是日期时间:
select username, avg(rating)
from rating
where YEAR(curdate()) = YEAR(rating.month)
and MONTH(curdate()) = MONTH(rating.month)
group by username
如果它是当月的前3个字符,请将WHERE语句替换为:
where LEFT(MONTHNAME(curdate()),3) = rating.month
答案 1 :(得分:2)
select username, avg(rating), month from RATING group by username,month
看到你有'JUL'作为你的月份,我假设它是一个varchar。如果你只想要JUL的结果:
select username, avg(rating) from RATING where month = 'JUL' group by username
答案 2 :(得分:1)
SELECT `username`, AVG(`rating`) AS average FROM RATING GROUP BY `username`