MYSQL - 选择每个玩家每天的最高分数

发布于 2024-12-07 20:25:17 字数 537 浏览 2 评论 0原文

假设我们有一个如下表,

  name   |  userid  |  score  |      datestamp      |
-----------------------------------------------------
  john   |    1     |   44    |  2011-06-10 14:25:55 
  mary   |    2     |   59    |  2011-06-10 09:25:51 
  john   |    1     |   38    |  2011-06-10 21:25:15 
  elvis  |    3     |   19    |  2011-06-10 07:25:18
  ...
  marco  |    4     |   100   |  2011-03-10 07:25:18  

我想显示每个用户每天的高分。

例如,如果玩家 john 在 2001 年 6 月 10 日打了十轮比赛,我想显示当天的最佳成绩,其他玩家依此类推。

非常感谢您抽出时间。

assuming we have a table like below

  name   |  userid  |  score  |      datestamp      |
-----------------------------------------------------
  john   |    1     |   44    |  2011-06-10 14:25:55 
  mary   |    2     |   59    |  2011-06-10 09:25:51 
  john   |    1     |   38    |  2011-06-10 21:25:15 
  elvis  |    3     |   19    |  2011-06-10 07:25:18
  ...
  marco  |    4     |   100   |  2011-03-10 07:25:18  

i want to display the high-score of each user in each single day.

so for eaxmple if the player john have played ten rounds in the 2001-06-10 i want to display the the best score of that day, and so on for the others players.

thank you very much for the time.

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

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

发布评论

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

评论(2

素手挽清风 2024-12-14 20:25:17
SELECT name, MAX(score) AS hiscore, DATE(datestamp) AS sdate
FROM scores
GROUP BY userid, sdate
ORDER BY sdate DESC

但还要注意,您的表甚至不在第一范式中,因为它缺少唯一的键。此外,修复此问题后,球员姓名将重复,并且必须将其提取到第二范式的单独表中。

标准化设计将有两个表:

player(userid, name)
scores(scoreid, userid, score, datestamp)
SELECT name, MAX(score) AS hiscore, DATE(datestamp) AS sdate
FROM scores
GROUP BY userid, sdate
ORDER BY sdate DESC

But also notice that your table is not even in the first normal form because it lacks an unique key. Also after fixing that, name of players will be repeated, and it will have to be extracted in a separate table for the second normal form.

A normalised design will have two tables:

player(userid, name)
scores(scoreid, userid, score, datestamp)
浮生面具三千个 2024-12-14 20:25:17

没有测试过,但是呢?

select userid, max(score), date(datestamp) from table
group by userid, date(datestamp)

Not tested, but what about?

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