MySQL数学获得前3名的分数

时间:2013-03-06 15:21:21

标签: mysql

我正在为Fantasy Movie League做一个网站,而我正在努力做的是获得前3名总分

总得分应为:每位玩家的有效电影的各点数之和除以该玩家的有效电影数量

(因此,如果玩家从活动电影中获得300分并且有3部活跃电影,答案应为100)

所以我的问题是:如何在不指定player_id的情况下计算单个玩家的活动电影数量(在子查询中)?

SELECT 
players.id as player_id,
first_name, last_name,
(metacritic + (imdb*10) + top_bottom + power(receipts,(2/9)) + ticket + oscar_noms + oscar_wins + gg_noms + gg_wins + isa_noms + isa_wins + razzie_noms + razzie_wins + festival_points + ifca_points) / (SELECT COUNT(id) FROM movies WHERE release_date >= CURDATE() and year_id=1 and player_id=4) as player_active_movie_total
FROM movies
INNER JOIN players on players.id=movies.player_id
WHERE release_date >= CURDATE() and year_id=1
ORDER BY player_active_movie_total DESC
LIMIT 3

1 个答案:

答案 0 :(得分:0)

您的select语句中不需要子查询。根据您的标准,您只需要使用GROUP BY语句和ID上的COUNT。下面的查询将不起作用,因为您需要对每列进行分组/求和,但应指向正确的方向

SELECT players.id as player_id, first_name, last_name,
(metacritic + (imdb*10) + top_bottom + power(receipts,(2/9)) + ticket + oscar_noms +     oscar_wins + gg_noms + gg_wins + isa_noms + isa_wins + razzie_noms + razzie_wins +     festival_points + ifca_points) / 
(COUNT(id)) as player_active_movie_total
FROM movies
 INNER JOIN players on players.id=movies.player_id
WHERE release_date >= CURDATE() and year_id=1
GROUP BY players.id, first_name, last_name
ORDER BY player_active_movie_total DESC
LIMIT 3

您的metacritic列(以及SELECT列表中的所有其他列)要么必须是聚合函数的一部分,即SUMCOUNTMAX,要么列在{{1}中像players.id这样的声明。我假设你想在整个专栏中使用某种SUM