sql - MySql ranking system with multiple categories -


this question has answer here:

on game ranking system, users can rate games using 3 categories (eg: cat1, cat2, cat3).

table game_ranking | id | user_id | game_id | cat1 | cat2 | cat3 | | 1  | 1       | 1       | 5    | 7    | 8    | | 2  | 1       | 2       | 10   | 8    | 5    | | 3  | 2       | 2       | 1    | 4    | 5    | | 4  | 3       | 1       | 5    | 7    | 8    | | 5  | 4       | 1       | 2    | 3    | 6    | | 6  | 7       | 3       | 6    | 6    | 3    | | 7  | 9       | 3       | 3    | 10   | 7    | 

the ranking must based on total of sum of each category column.

eg: game_id 2 score = cat1(10 + 1) + cat2(8 + 4) + cat3(5 + 5). game_id 2 score 33.

if 2 or more games have same score, game has highest number of votes should on top of other.

i need mysql query create ranking based on these criterias mentioned above.

thanks!

a straight forward sum/group need;

select game_id, sum(cat1+cat2+cat3) score, count(*) votes game_ranking group game_id order score desc, votes desc 

an sqlfiddle test with.

the query sums categories , counts number of votes, grouping game_id. after that, it's simple matter or ordering need.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -