如何让SQL查询排名?

发布于 2024-11-08 23:33:18 字数 865 浏览 2 评论 0原文

考虑到最佳时间和得分,如何获得有史以来最好的排名?假设用户赢了几次,如何计算最佳用户赢了多少次以及平均赢的时间是多少?

+----------------+----------------+--------+-----------------------+
| user_id        | quiz_id        | score  | finish                |
+----------------+----------------+--------+-----------------------+
| 1              | 1              | 1      | 2011-05-18 21:39:00   |
| 2              | 1              | 1      | 2011-05-18 21:43:10   |
| 3              | 1              | 0      | 2011-05-18 21:40:55   |
| 1              | 2              | 1      | 2011-05-18 22:51:57   |
| 2              | 2              | 1      | 2011-05-18 22:21:37   |
| 3              | 2              | 0      | 2011-05-18 22:22:48   |
| 4              | 2              | 1      | 2011-05-18 22:58:14   |              
+----------------+----------------+--------+-----------------------+

How to get the ranking best of the best ever, considering the best time and the score? Assuming that user won a few times, how to count how many times best user wins and what is the average time of this wins?

+----------------+----------------+--------+-----------------------+
| user_id        | quiz_id        | score  | finish                |
+----------------+----------------+--------+-----------------------+
| 1              | 1              | 1      | 2011-05-18 21:39:00   |
| 2              | 1              | 1      | 2011-05-18 21:43:10   |
| 3              | 1              | 0      | 2011-05-18 21:40:55   |
| 1              | 2              | 1      | 2011-05-18 22:51:57   |
| 2              | 2              | 1      | 2011-05-18 22:21:37   |
| 3              | 2              | 0      | 2011-05-18 22:22:48   |
| 4              | 2              | 1      | 2011-05-18 22:58:14   |              
+----------------+----------------+--------+-----------------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2024-11-15 23:33:18

考虑到最近的完成日期时间来获得最佳分数非常容易。

总体

SELECT user_id, quiz_id, score, finish
FROM table
ORDER BY score DESC, finish DESC
LIMIT 1

对于每个测验

SELECT user_id, quiz_id, score, finish
FROM table
GROUP BY quiz_id
ORDER BY score DESC, finish DESC

对于每个用户

SELECT user_id, quiz_id, score, finish
FROM table
GROUP BY user_id
ORDER BY score DESC, finish DESC

我不确定您所说的用户获胜数量是什么意思。胜利意味着什么?与平均时间相同;这里没有足够的信息来帮助您解决这两个问题。

To get the best score considering the latest finish datetime is pretty easy.

Overall

SELECT user_id, quiz_id, score, finish
FROM table
ORDER BY score DESC, finish DESC
LIMIT 1

For Each Quiz

SELECT user_id, quiz_id, score, finish
FROM table
GROUP BY quiz_id
ORDER BY score DESC, finish DESC

For Each User

SELECT user_id, quiz_id, score, finish
FROM table
GROUP BY user_id
ORDER BY score DESC, finish DESC

I'm not sure what you mean by number of user wins. What signifies a win? Same with average time; there's not enough information here to help you with these two questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文