I’m making a system that users earn achievements with certain actions on the site.
The database is divided into three, the user, achievements and table linking the two.
For the ranking what is important is the table that links users and achievements.
Structured like this (called user_achievements):
| id | user_id | achievement_id | created_at | updated_at |
On top ranking should be the person with the most achievements and who won that first.
The query that was assembled:
SELECT user_id,
COUNT(achievement_id) AS 'total',
created_at
FROM `user_achievements`
GROUP BY user_id ORDER BY total DESC, created_at ASC LIMIT 10
This query works, but the problem is the “group by” for who has the most achievements (for the “total”), it does not back the last created_at user. Come the first created_at that user, which does not fit sort who was the first to earn that amount of achievements.
My system uses Ruby on Rails, is there a solution to this query in ActiveRecord, would be of great help.
Thanks a lot!
Sorry for my english…
1 Answer