I know this is a popular topic, but I still haven't found quite what I'm looking for. I'd like to query one table
BOOKS_READ
id
user_id
book_id
to format a leaderboard of users who have listed the most books as having been read. When a user reads a book, a record matching the book id and the user id gets logged into the books_read table.
Is it possible to rank the results of this query, starting at 1, and with consideration of ties?
SELECT user_id, COUNT(*) AS book_count
FROM books_read
GROUP BY user_id
ORDER BY book_count DESC LIMIT 10
In the event of a tie, I would like to list an '=' sign in the results.
For example,
rank user_id book_count
=1 30 121
=1 17 121
2 101 119
=3 11 104
=3 91 104
Many thanks for any help! I don't mind using PHP to handle some of it, but I'm very interested in learning straight SQL solutions to these kinds of things :-)