I have a table of items that users are allowed to vote on. In this table, there is a votes column, that holds the number of votes that item has accumulated, and a rank column, that is a ranking of all items based on the number of votes they have (i.e. most votes gets rank 1, second most gets rank 2, etc.)
Currently, I’m recalculating the rank of every item after every vote. That is, when a user votes, I add one to that item’s votes column, and then update every rank with the following query:
SET @rank = 0
UPDATE items SET rank = @rank := @rank + 1 ORDER BY votes DESC
This works for the most part, but doesn’t take in to account voting ties. If I have votes [10, 4, 3, 0], I would expect ranks [1, 2, 3, 4]. However, if I have votes [10, 10, 3, 0], I would like ranks [1, 1, 3, 4]. This doesn’t happen; I still get ranks [1, 2, 3, 4].
How can I incorporate ties like I’ve described above?
This inelegant solution would return what you desire: