MySQL:查询有平局的排行榜
我知道这是一个流行的话题,但我仍然没有找到我想要的东西。我想查询一个表
BOOKS_READ
id
user_id
book_id
来格式化列出已阅读书籍最多的用户的排行榜。当用户阅读一本书时,与书籍 ID 和用户 ID 匹配的记录将记录到 books_read 表中。
是否可以对这个查询的结果进行排名,从 1 开始,并考虑到平局?
SELECT user_id, COUNT(*) AS book_count
FROM books_read
GROUP BY user_id
ORDER BY book_count DESC LIMIT 10
如果出现平局,我想在结果中列出一个“=”符号。
例如,
rank user_id book_count
=1 30 121
=1 17 121
2 101 119
=3 11 104
=3 91 104
非常感谢您的帮助!我不介意使用 PHP 来处理其中的一些问题,但我对学习此类事情的直接 SQL 解决方案非常感兴趣:-)
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 :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给你
它然后你可以使用 PHP 来解析关系。
Giving you
Which you can then use PHP to parse for ties.
你要做的就是统计有多少人的分数比你感兴趣的记录更好,然后加 1。
所以,如果你是最好的玩家,那么 0 个人有更好的分数,所以 0 + 1 = 排名第一。
如果五个人的得分更高,无论其中有多少人相互并列,您仍然是 5 + 1 = 6。
问题是,对于您显示的每一行来说,这是一种昂贵的查询。因此,您可能希望对结果集中的第一行执行此操作,然后在表示层 (PHP) 中为之后的每个行添加一个。执行此操作时,请务必考虑关系。
边缘条件是你不知道结果集中的第一行是否与“他”之前的人相关。因此,您需要知道有多少人与结果集中的第一个人得分相同。 (如果您从顶部开始,这不是问题。)
What you want to do is count the number of people who have a better score than the record you're interested in, and add 1.
So, if you're the best player, zero people have a better score, so 0 + 1 = #1 ranking.
If five people have a better score, regardless of how many of them tied with each other, you're still 5 + 1 = 6.
The problem is that this is kind of an expensive query to do for every row you display. So you probably want to do it for the first row in your result set and then add one for everyone after that in your presentation layer (PHP). Be sure to take ties into consideration as you do this.
And the edge condition is that you don't know if the first row in your result set is tied with people ahead of "him". So you need to know how many people have the same score as the first person in your result set. (If you're starting at the top, this isn't a problem.)